[英]Watch if an object has changed
Is it possible to execute some code each time an object has changed ? 每次对象发生变化时都可以执行一些代码吗?
var a = {x: 12, y: 63};
a.watch(function () { console.log('The object has changed!'); });
a.x = 16; // The object has changed!
a.y = 1; // The object has changed!
It seems to be available on Firefox only: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/watch 它似乎仅适用于Firefox: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/watch
How is it possible to achieve this in JavaScript / node.js? 如何在JavaScript / node.js中实现这一点? (if possible, without using a getter/setter method)
(如果可能,不使用getter / setter方法)
You can use Object Observe ... it's experimental so proceed with caution... 您可以使用Object Observe ...它是实验性的,所以请谨慎行事......
var obj = {
foo: 0,
bar: 1
};
Object.observe(obj, function(changes) {
console.log(changes);
});
obj.baz = 2;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.