let target={} let handler={ set(target,proName,proValue,receiver){ //确认对象的属性赋值成功 let isSuccess=Reflect.set(target,proName,proValue,receiver) if(isSuccess){ console.log("成功") } return isSuccess } } let proxy=newProxy(target,handler)
确保对象的属性能正确赋值,广义上讲,即确保对象的原生行为能够正常进行,这就是Reflect的作用
二、Reflect的API
注:和Proxy的API一致
2.1 Reflect.get(target,property,receiver)
查找并返回target对象的property属性
let obj={ name:"poetries", } let result=Reflect.get(obj,"name") console.log(result) //poetries
let obj={ //属性yu部署了getter读取函数 getyu(){ //this返回的是Reflect.get的receiver参数对象 returnthis.name+this.age } }
let receiver={ name:"shen", age:"18", }
let result=Reflect.get(obj,"yu",receiver) console.log(result) //shen18
// 旧写法 let small= Math.min.apply(Math, array) //1 let big = Math.max.apply(Math, array) //6 let type = Object.prototype.toString.call(small) //"[object Number]"
// 新写法 const small= Reflect.apply(Math.min, Math, array) const big = Reflect.apply(Math.max, Math, array) //第三个参数是Object类型的就好,因为调用的是Object的原型方法toString const type = Reflect.apply(Object.prototype.toString, small, [])