From b1f12d018c8dd37e2febdb9af6c64d74f746bdff Mon Sep 17 00:00:00 2001 From: ser Date: Sun, 5 Jan 2020 22:04:18 +0200 Subject: [PATCH] Loop for moved out from constructor of class Logable --- JavaScript/3-class.js | 56 ++++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 27 deletions(-) diff --git a/JavaScript/3-class.js b/JavaScript/3-class.js index 2b06e1f..62132fa 100644 --- a/JavaScript/3-class.js +++ b/JavaScript/3-class.js @@ -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