- 实现原理:
1.通过Proxy(代理):拦截对象中任意属性的变化:包括属性值得读写,属性的添加,属性的删除等
2.通过Reflect (反射) :对被源对象的属性进行操作
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
let person = {
name : '方敬',
age:18
}
const p = new Proxy(person,{
get(target,prop){
return Reflect.get(target,prop)
},
set(target,prop,value){
return Reflect.set(target,prop,value)
},
deleteProperty(target,prop){
return Reflect.get(target,prop)
}
})
</script>
</body>
</html>
- 测试
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/dded6f3f4e15943b7c6a90746fb62aad.png)
![List item](https://i-blog.csdnimg.cn/blog_migrate/6b1bf9319259a94d388434fd99f6eb66.png)