<script>
const obj = {
name:'Li',
age:18,
gender:'man'
}
Object.keys(obj).forEach(key => {
Object.defineProperty(obj, key, {
get() {
console.log(`obj中的${
key}的值被获取`);
},
set() {
console.log(`obj中的${
key}的值被设置`);
}
})
})
console.log(obj.name);
obj.age = 20
script>
缺点:
与设计初衷不符
难以进行增加和删除操作的监听
proxy是一个类
实例是代理对象
<script>
const obj = {
name:'Li',
age:18,
gender:'man'
}
const proxyObj =