我有变量,例如var obj = { foo: 'fooval', bar: 'barval'}
如何编写事件和触发器来检测obj
的任何修改,然后在控制台中调用信息,如下所示:console.log('changed obj.key from oldVal to newVal')
;
答案 0 :(得分:4)
有新功能Object.observe
,注意 - this feature supported only in Chrome
var obj = { foo: 'fooval', bar: 'barval'};
Object.observe(obj, function(changes) {
console.log(changes);
});
obj.foo = 'bar';
还有 polyfills ,例如Object.observe