http://www.oschina.net/code/snippet_1590754_46481
//简单对象的属性的变化监控 //通过setAttr改变属性的值 var o = { 'a':2, 'b':3 }; function watch(obj, attr, callback){ if(typeof obj.defaultValues == 'undefined'){ obj.defaultValues = {}; for(var p in obj){ if(typeof obj[p] !== 'object') obj.defaultValues[p] = obj[p]; } } if(typeof obj.setAttr == 'undefined'){ obj.setAttr = function(attr, value){ if(this[attr] != value){ this.defaultValues[attr] = this[attr]; this[attr] = value; return callback(this); } return this; }; } } watch(o, 'a', function(obj){ console.log(obj); }); o.setAttr('a',4);