Skip to content
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
32 changes: 32 additions & 0 deletions 2019/Aug/Week4/reactive-variables.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,38 @@
*/
module.exports = class ReactiveVariables {
constructor(variables) {
const result = {};
const variablesObj = Object.assign({}, variables);

Object.keys(variables).forEach(key => {
if(typeof variables[key] === 'function') {
result[key] = variables[key].call(variablesObj);
variablesObj[key] = result[key];
} else {
Object.defineProperty(result, key, {
get: () => variablesObj[key],
set: (val) => {
variablesObj[key] = val;

Object.keys(result).forEach(k => {
if(typeof result[k] === 'object') {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixme:这里的处理逻辑很不严谨

if(Object.keys(result[k]).indexOf(key) > -1) {
result[k] = variables[k].call(variablesObj);
variablesObj[k] = result[k];
}
} else if(typeof variables[k] === 'function') {
if(variables[k].call(variablesObj) !== result[k]) {
result[k] = variables[k].call(variablesObj);
variablesObj[k] = result[k];
}
}
})
},
})
result[key] = variables[key];
}
})

return result;
}
}