- Proxy 可以给我们的变量加上众多代理, 从而达成我们的目的, 而不需要修改obj自身
function sum(a, b) {
return a + b;
}
const handler = {
// 用于拦截函数的调用
apply: function(target, thisArg, argumentsList) {
console.log(`Calculate sum: ${argumentsList}`);
// expected output: "Calculate sum: 1,2"
return target(argumentsList[0], argumentsList[1]) * 10;
}
};
const proxy1 = new Proxy(sum, handler);
console.log(sum(1, 2));
// expected output: 3
console.log(proxy1(1, 2));
// expected output: 30
其他常用的handler属性