let targetObj = {
age: 18,
}
function Observer(oldval,newval){
console.info('name属性的值从 '+ oldval +' 改变为 ' + newval);
}
Object.defineProperty(targetObj, 'name', {
enumerable: true,
configurable: true,
get: function() {
return name
},
set: function(val) {
Observer(name, val)
name = val
}
})
targetObj.name = 'zhang'
targetObj.name = 'yang'
function Observer1(oldval,newval){
console.info('name属性的值从 '+ oldval +' 改变为 ' + newval);
}
class TargetObj {
constructor(name, age){
this.name = name
this.age = age
}
set name (val) {
Observer1(name, val)
name = val
}
}
let tar = new TargetObj('yi', 18)
tar.name = 'china'
console