用法:

1
2
3
4
5
6
var obj = {_name:"test"}
obj.bind("name",function(){
console.log("callback");
})

obj.name = "测试";    // 会触发bind方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
if(!Object.prototype.bind){
Object.prototype.bind = function(key, callback){
var _key = "_" + key;
Object.defineProperty(this, key, {
get: function(){
return this[_key];
},
set: function(value){
if(value !== this[_key]){
this[_key] = value;
callback();
}
}
});
}
}