如何检测对象变量是否已更改? [英] How to detect if an object variable has been changed?

查看:86
本文介绍了如何检测对象变量是否已更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代表栅栏的类,它内部由矩形和圆形标记对象(也是我的类)组成。围栏有4个变量 - x1,x2,y1和y2。如果有任何这些更改,我必须修改或重建内部标记对象。

I have a class that represents a fence, internally its made up from rectangle and circle marker objects (also my classes). The fence has 4 variables - x1, x2, y1 and y2. If any of these changes I have to modify or rebuild the internal marker objects.

存储和检查4个值并不是什么大不了的事,但这只是我的第一个世界对象类,而且会有更长的变量列表。
有没有什么好方法可以检查是否有任何更改或触发更改的内容而不显式存储double值并在每次重绘画布时进行检查?像vb.net中的属性那样的东西?

Storing and checking the 4 values isn't such a big deal but this is just the first of my world object classes and there will be ones with much longer lists of variables. Is there any good way of checking whether any of these has changed or trigger something on change without explicitly storing double values and checking each time the canvas is redrawn? Something like a property in vb.net or such?

推荐答案

var fence= {
   set x1(){
      alert('change');
      this.rebuild();
   },
   rebuild: function(){}
}

function Fence(val){
    var value = val;

    this.__defineGetter__("x1", function(){
        return value;
    });

    this.__defineSetter__("x1", function(val){
        alert('change');
        this.rebuild();
    });
    this.rebuild = function(){};
}
var fence = new Fence();

这篇关于如何检测对象变量是否已更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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