JSモニター変数の変化
1044 ワード
個別属性変化モニター:Object.defineProperty(obj,prop,descriptor)
// a age
var a = { age: 10 };
Object.defineProperty(a, 'age', {
get: function() { //
console.log('get: ', age);
return age;
},
set: function(value) { //
age = value;
console.log('set: ', age);
}
});
a.age; //get: 10
a.age = 20; //set: 20
複数の属性変化モニター:Object.defineProperties(obj,props)var obj = {};
Object.defineProperties(obj, {
a: {
configurable: true,
get: function() {
console.log('get: ', a);
return a;
},
set: function(value) {
a = value;
console.log('set: ', a);
}
},
b: {
configurable: true,
get: function() {
console.log('get: ',b);
return b;
},
set: function(value) {
b = value;
console.log('set: ', b);
}
}
});