Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loop for moved out from constructor of class Logable #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 29 additions & 27 deletions JavaScript/3-class.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,37 @@
'use strict';

const logable = fields => class Logable {
constructor(data) {
this.values = data;
for (const key in fields) {
Object.defineProperty(Logable.prototype, key, {
get() {
console.log('Reading key:', key);
return this.values[key];
},
set(value) {
console.log('Writing key:', key, value);
const def = fields[key];
const valid = (
typeof value === def.type &&
def.validate(value)
);
if (valid) this.values[key] = value;
else console.log('Validation failed:', key, value);
}
});
const logable = fields => {
class Logable {
constructor(data) {
this.values = data;
}
}

toString() {
let result = this.constructor.name + '\t';
for (const key in fields) {
result += this.values[key] + '\t';
toString() {
let result = this.constructor.name + '\t';
for (const key in fields) {
result += this.values[key] + '\t';
}
return result;
}
return result;
}
for (const key in fields) {
Object.defineProperty(Logable.prototype, key, {
get() {
console.log('Reading key:', key);
return this.values[key];
},
set(value) {
console.log('Writing key:', key, value);
const def = fields[key];
const valid = (
typeof value === def.type &&
def.validate(value)
);
if (valid) this.values[key] = value;
else console.log('Validation failed:', key, value);
}
});
}
return Logable;
};

// Usage
Expand Down