handler与proxy,target的用法

function sum(a, b) {
    
    
    return a + b;
}

const handler = {
    
    
    apply: function(target, thisArg, argumentsList) {
    
    
        console.log(`Calculate sum: $(argumentsList)`);
        return target(argumentsList[0], argumentsList[1]) * 10;
    }
};

const proxy1 = new Proxy(sum, handler);

console.log(sum(1, 2))
console.log(proxy1)
console.log(proxy1(1, 2))
var p = new Proxy(function() {
    
    }, {
    
    
    apply: function(target, thisArg, argumentsList) {
    
    
      console.log('called: ' + argumentsList.join(', '));
      return argumentsList[0] + argumentsList[1] + argumentsList[2];
    }
  });
  
  console.log(p(1, 2, 3))

https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Proxy/Proxy/apply

猜你喜欢

转载自blog.csdn.net/weixin_40945354/article/details/120131819