/**
* apply,拦截函数的调用
*///参数varadd=(a, b)=> a + b
var p =newProxy(add,{apply(target, thisArg, argumentsList){
target === add //true
thisArg === p //true
argumentsList //[1,2]}})
p.call(p,1,2)//三种调用:proxy(),Function.prototype.apply/call,Reflect.applyvar p =newProxy((a, b)=> a +10* b,{apply(target, thisArg, argumentsList){returntarget(...argumentsList)}})p(1,2)//21
Reflect.apply(p,null,[1,2])//21
p.call(null,1,2)//21
p.apply(null,[1,2])//21
//数据校验functionvalidator(target, validator){returnnewProxy(target,{set(target, key, value, receiver){if(target.hasOwnProperty(key)){if(validator[key](value)){return Reflect.set(target, key, value, receiver)}else{throwError(`不能设置${key}到${value}`)}}else{throwError(`${key}不存在`)}}})}classPerson{constructor(name, age){this.name = name
this.age = age
returnvalidator(this,{name(val){returntypeof val ==='string'},age(val){returntypeof val ==='number'&& val >18}})}}var person =newPerson('knyel',30)
person
person.name ='lk'// person.age = 12 //报错