每次变量更改时如何在 JavaScript 中运行函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5282507/
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
How to run a function in JavaScript every time a variable changes?
提问by VerizonW
Is there a way in JavaScript to have something like an event that listens to changes in a variable? so when its value is modified the event triggers and then I can call a function. To put this more into context I have a function which handles the html rendering of an array of objects, and I want that function to be called automatically every time the array is modified.
JavaScript 中有没有办法让事件类似于监听变量变化的事件?所以当它的值被修改时,事件会触发,然后我可以调用一个函数。为了将其更多地放在上下文中,我有一个处理对象数组的 html 渲染的函数,并且我希望每次修改数组时自动调用该函数。
Thanks.
谢谢。
采纳答案by Gabriele Petrioli
Use object.watchdocsand if it is not supported natively look at this implementation: Object.watch() for all browsers?
使用object.watch文档,如果本机不支持,请查看此实现:Object.watch() for all browsers?
回答by Pascal MARTIN
I don't think what you ask is possible.
我不认为你问的是可能的。
A solution might be to :
解决方案可能是:
- encapsulate your data into a specific object
- access that data using a setter method of that object
- have that setter method both :
- set the data
- call your function
- 将您的数据封装到特定对象中
- 使用该对象的 setter 方法访问该数据
- 有那个 setter 方法:
- 设置数据
- 调用你的函数
But it'll require you to rewrite a bit of your code.
但这需要您重写一些代码。
回答by xanatos
In ECMAScript 5 there are getter/setter properties... Read here: http://ejohn.org/blog/ecmascript-5-objects-and-properties/
在 ECMAScript 5 中有 getter/setter 属性...阅读此处:http: //ejohn.org/blog/ecmascript-5-objects-and-properties/
Non-IE browsers support something similar:
非 IE 浏览器支持类似的东西:
http://robertnyman.com/2009/05/28/getters-and-setters-with-javascript-code-samples-and-demos/
http://robertnyman.com/2009/05/28/getters-and-setters-with-javascript-code-samples-and-demos/
For IE, you'll have to wait for IE9, or use only DOM-bases getters/setters.
对于 IE,您必须等待 IE9,或者仅使用基于 DOM 的 getter/setter。
回答by Andrew
Because JavaScript doesn't universally support setter/getter methods yet, I'd recommend you think about how you set your variables. One technique that would work is:
由于 JavaScript 尚未普遍支持 setter/getter 方法,因此我建议您考虑如何设置变量。一种可行的技术是:
Array.prototype.setMember = function(index,newValue) {
alert("I will perform some action here");
this[index] = newValue;
}
var myArray = [1,2,3];
// x[0] = 11; // Don't do this any more
x.setMember(0,11);
alert(x[0]);
I'm personally not a huge fan of adding new methods to base prototypes, but it makes things easier to refactor in the short term.
我个人不是很喜欢向基础原型添加新方法,但它使短期内更容易重构。
回答by pimvdb
You could use setIntervalto check for its value so many times a second, and save it into a separate variable. You can check each time whether the real variable is different from the other one. In that case, call the function.
您可以使用setInterval每秒多次检查其值,并将其保存到单独的变量中。您可以每次检查实际变量是否与另一个不同。在这种情况下,调用该函数。
It's a dirty trick, though.
不过,这是一个肮脏的把戏。

