用Object.defineProperty和new Proxy来实现给对象name 赋值 如果是‘张三’ 复制成“张三name”


let b = {
}
let b_a
Object.defineProperty(b, 'name', {
configurable : true,
enumerable : true,
get:function(value) {
console.log('get')
return b_a
},
set: function(value) {
console.log('set', value)
if (value == '张三') {
console.log('sss')
b_a = '张三+name'
console.log('b_a11',b_a)
}
}
}
)
console.log(b.name = '张三')
console.log('a', JSON.stringify(b))



let b = {
name: ''
}

let p = new Proxy(b, {
get: function(target, key, value) {
console.log('get', target, key, value)
return target[key]
},
set: function(target, key, value) {
if (key == 'name' && value == '张三') {
target[key] = '张三name'
} else {
target[key] = value
}
console.log('set', target, key, value)
},
})
console.log(p.name = "张三", b)