Javascript跟踪变量更改 [英] Javascript Track Variable Change
问题描述
是否有可能跟踪javascript中的变量更改?我希望能够在类中变量发生变化时启动函数。
< script>
var mywindow = new Box();
mywindow.title =newtitle;
//启动mywindow.applyTitle()并更改html
< / script>
您可以使用明确的方法来设置值并附上你的回调。有些东西可以工作:
$ b $ pre $ var Box = new Class({
setTitle:function(title){
this.title = title;
//现在调用你的回调函数,你可能需要一些方法来注册它们btw
});
改变想法以适合您的类语法...
JS的getter / setter语法也不太好(相当于Python中的属性)。请参阅 http://ejohn.org/blog/javascript-getters-and-setters/ 。
Is it possible to track a variable change in javascript? I would like to be able to launch a function when a variable has been changed in a class.
<script>
var mywindow = new Box();
mywindow.title = "newtitle";
//Launches mywindow.applyTitle() and changes html
</script>
You could probably use an explicit method for setting the value and attach your callbacks to that. Something along this would work:
var Box = new Class({
setTitle: function(title) {
this.title = title;
// call your callbacks now. you probably need some way to register them btw
}
});
Adapt the idea to fit your class syntax...
There is also a not so well supported getter/setter syntax for JS (equivalent to properties in Python). See http://ejohn.org/blog/javascript-getters-and-setters/ .
这篇关于Javascript跟踪变量更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!