Javascript跟踪变量更改 [英] Javascript Track Variable Change

查看:125
本文介绍了Javascript跟踪变量更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能跟踪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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆