正文开始 <pre>if (!Object.prototype.watch)
{
Object.prototype.watch = function (prop, handler)
{
var oldval = this[prop], newval = oldval,
getter = function ()
{
return newval;
},
setter = function (val)
{
oldval = newval;
return newval = handler.call(this, prop, oldval, val);
};
if (delete this[prop])
{
if (Object.defineProperty) // ECMAScript 5
{
Object.defineProperty(this, prop, {get: getter,set: setter});
}
else if (Object.prototype.__defineGetter__ && Object.prototype.__defineSetter__)
{
Object.prototype.__defineGetter__.call(this, prop, getter);
Object.prototype.__defineSetter__.call(this, prop, setter);
}
}
};
}
if (!Object.prototype.unwatch)
{
Object.prototype.unwatch = function (prop)
{
var val = this[prop];
delete this[prop];
this[prop] = val;
};
}</pre><p><br /></p><p><br /></p><p><pre>Object.defineProperty(someObj, 'property', {
get: function () {
console.log(this);
return this.val;
},
set: function (vals) {
this.val = vals;
}
});</pre> <br /></p>
正文结束 |
js 正则替换返回值做回调函数 | jQ给dom绑定属性/动作 |