Reflect的作用,target,property,value,receiver代表啥

1.真的proxy

复制代码
let target ={name:'张三'}
let handler = {
  get(target,property,receiver){
    console.log(1,target,2,property,3,receiver)
    return Reflect.get(target,property,receiver)
  },
  set(target,property,value,receiver){
    console.log('a',target,'b',property,'c',value,'d',receiver)
    return Reflect.set(target,property,value,receiver)
  }
}
let proxy = new Proxy(target,handler)
console.log(proxy.name)
proxy.name = '李四'
// console.log(proxy.name)

以上代码用浏览器控制台运行下如下

可知target,property,value,receiver的值

不知道为啥这里的target换成多层的对象就不起作用了

复制代码
let target ={name:'张三',info:{age:18,sex:'男'}}
console.log(proxy.info.sex)
proxy.info.sex = '女'

2.自己写的proxy

很明显这里的reflect.get被return target[key]替代,

reflect.set被 '=' 替代

说明了reflect在proxy的作用

复制代码
let handler = {
  get(target, key, receiver) {
    console.log(1, target, 2, key, 3, receiver)
    return target[key];
  },
  set(target, key, value, receiver) {
    console.log('a', target, 'b', key, 'c', value, 'd', receiver)
    target[key] = value;
    return true;
  },
};
let target ={name:'张三'}
let proxy = new Proxy(target, handler);
console.log(proxy.name)
proxy.name = '李四'
console.log(proxy.name)
相关推荐
快起来别睡了几秒前
手写 Ajax 与 Promise:从底层原理到实际应用
前端
打不着的大喇叭1 小时前
uniapp的光标跟随和打字机效果
前端·javascript·uni-app
无我Code1 小时前
2025----前端个人年中总结
前端·年终总结·创业
程序猿阿伟1 小时前
《前端路由重构:解锁多语言交互的底层逻辑》
前端·重构
Sun_light1 小时前
6个你必须掌握的「React Hooks」实用技巧✨
前端·javascript·react.js
爱学习的茄子1 小时前
深度解析JavaScript中的call方法实现:从原理到手写实现的完整指南
前端·javascript·面试
莫空00001 小时前
Vue组件通信方式详解
前端·面试
呆呆的心1 小时前
揭秘 CSS 伪元素:不用加标签也能玩转出花的界面技巧 ✨
前端·css·html
百锦再1 小时前
重新学习Vue中的按键监听和鼠标监听
javascript·vue.js·vue·计算机外设·click·up·down
快起来别睡了1 小时前
Vue 3 中的组件通信与组件思想详解
vue.js