| 
x
加入我们,或者,欢迎回来。您需要 登录 才可以下载或查看,没有帐号?注册会员  Object.prototype.attr_accessor = function (name,callBack) {        this[name] = function (value) {                if (value !== undefined) {                        this['_'+name] = value;                        callBack();                        return this;                } else {                        return this['_'+name];                };        };        return this;}; Object.prototype.attr_reader = function (name) {        var name = arguments[i];        this[name] = function (value) {                return this['_'+name];        };        return this;                } Object.prototype.attr_writer = function (name) {        this[name] = function (value) {                if (value !== undefined) {                        this['_'+name] = value;                        return this;                } else {                        return undefined;                };        };        return this;}
Object.prototype.attr_accessor = function (name,callBack) { 
        this[name] = function (value) { 
                if (value !== undefined) { 
                        this['_'+name] = value; 
                        callBack(); 
                        return this; 
                } else { 
                        return this['_'+name]; 
                }; 
        }; 
        return this; 
}; 
  
Object.prototype.attr_reader = function (name) { 
        var name = arguments[i]; 
        this[name] = function (value) { 
                return this['_'+name]; 
        }; 
        return this;                 
} 
  
Object.prototype.attr_writer = function (name) { 
        this[name] = function (value) { 
                if (value !== undefined) { 
                        this['_'+name] = value; 
                        return this; 
                } else { 
                        return undefined; 
                }; 
        }; 
        return this; 
} 
顺手造了个轮子。类似于ruby的attr_XXX系列方法。带参数就是修改,不带参数就是访问。其中attr_accessor中的callBack是传入一个函数,在属性值被修改的时候触发的回调事件
 要注意的是不能再回调事件里写对该事件的修改。不然会成死循环。多个属性的话也不能成环。
 另外,对属性的访问和修改可以通过调用"_"+属性名来直接访问
 
 |