construct
construct用于拦截new命令
两个参数,target:目标对象,args:构造函数的参数对象
let handler = {
construct(target,args,newTargets){
return new target(...arguments);
}
}
// eg1
let p = new Proxy(function(){},{
construct : function(target,args){
console.log('called: ' + args.join(', '));
return { value : args[0] * 10 };
}
});
console.log((new p(1)).value);
construct方法返回的必须是一个对象,否则会报错
let p2 = new Proxy(function(){},{
construct : function(target,argumentsList){
return 1;
}
});
// 'construct' on proxy: trap returned non-object ('1')
// new p2(); 报错