检测 Javascript 对象的变化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11578377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Detecting change in a Javascript Object
提问by Unitech
I found this gist to detect changes on specified fiels of a object : https://gist.github.com/3138469
我发现这个要点可以检测对象的指定字段的变化:https://gist.github.com/3138469
But it bind an event only on one field.
但它只在一个字段上绑定一个事件。
Someone know a function or a tricks to detect change on an entire Javascript Object ?
有人知道检测整个 Javascript 对象变化的函数或技巧吗?
回答by Adi
2019 Update:These days this can be achieved using Proxy APIin a much more efficient manner. The on-change libraryuses the Proxy API behind the scene to make this even easier.
2019 年更新:现在可以使用代理 API以更有效的方式实现这一点。在上改变图书馆使用幕后代理API,使这个更容易。
2012 Update:I've just noticed that the author of Watch.js is referencing a library with much broader browsers' support. MultiGetSet.JS
2012 年更新:我刚刚注意到 Watch.js 的作者引用了一个具有更广泛浏览器支持的库。MultiGetSet.JS
When I want to achieve this I usually use Watch.js, you can watch a whole object or one attribute.
当我想实现这一点时,我通常使用Watch.js,您可以查看整个对象或一个属性。
回答by Bergi
This is currently not possible, you can only define getters/setters for individual properties.
这目前是不可能的,您只能为单个属性定义 getter/setter。
Yet, there is a draft for Proxy
objects which could do that. Today, it is only supported in Firefox' Javascript 1.8.5.
然而,有一个Proxy
可以做到这一点的对象草案。今天,它仅在 Firefox 的 Javascript 1.8.5 中受支持。
回答by user2820213
This is not possible using watch.js. it just detect the changes of an object or the attributes of the object. It DOESNT detect if we add an attribute to the object or change a new added attribute...
使用 watch.js 是不可能的。它只是检测对象的变化或对象的属性。它不会检测我们是否向对象添加属性或更改新添加的属性...