Proxy 代理 ES6增强 对象和函数(方法) 生命周期 预处理 get set apply 方法
let obj = {
add: function (val) {
return val + 100
},
name: 'js'
}
console.log(obj.add(100));
console.log(obj.name);
// get set
let pro = new Proxy({
add: function (val) {
return val + 100
},
name: 'js'
}, {
get: function (target, key, property) {
console.log('Get');
return target[key]
},
set: function (target, key, value, relelver) {
console.log(`${key}=${value}`);
return target[key] = value+'123'
}
})
console.log(pro.name);
pro.name = '技术'
console.log(pro.name);
// apply
let targer=function(){
return 'targer'
}
let handler={
apply(targer,ctx,args){
console.log('do apply');
return Reflect.apply(...arguments)
}
}
let pro=new Proxy(targer,handler)
console.log(pro());