Is it possible to have an event in JS that fires when the value of a certain variable changes?(某个变量的值更改时,是否可能在JS中触发事件?)
Yes, this is now completely possible!(是的,现在完全有可能!)
aInternal
a
x = { aInternal: 10, aListener: function(val) {}, set a(val) { this.aInternal = val; this.aListener(val); }, get a() { return this.aInternal; }, registerListener: function(listener) { this.aListener = listener; } }
x.registerListener(function(val) { alert("Someone changed the value of x.a to " + val); });
xa
x.a = 42;
1.4m articles
1.4m replys
5 comments
57.0k users