From 1675066df38e50cd52eed7f6b8df0af8d67c8bc7 Mon Sep 17 00:00:00 2001 From: lg Date: Tue, 27 Aug 2019 18:14:38 +0800 Subject: [PATCH] save --- 2019/Aug/Week4/reactive-variables.js | 29 +++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/2019/Aug/Week4/reactive-variables.js b/2019/Aug/Week4/reactive-variables.js index 15981f3..e0d4dd1 100644 --- a/2019/Aug/Week4/reactive-variables.js +++ b/2019/Aug/Week4/reactive-variables.js @@ -3,6 +3,33 @@ */ module.exports = class ReactiveVariables { constructor(variables) { - + const obj = {}; + const variablesObj = {}; + Object.keys(variables).forEach(key => { + if(typeof variables[key] === 'function') { + Object.defineProperty(obj, key, { + get: () => { + const value = variables[key].call(obj); + if(typeof variablesObj[key] === 'object') { + let shouldNew = false; + Object.keys(value).forEach(k => { + if(variablesObj[key][k] !== value[k]) { + shouldNew = true; + } + }); + if(shouldNew) { + variablesObj[key] = value; + } + }else { + variablesObj[key] = value; + } + return variablesObj[key]; + } + }); + }else { + obj[key] = variables[key]; + } + }); + return obj; } }