首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Proxy.handler.apply

handler.apply()?方法用于拦截函数的调用。

语法

代码语言:javascript
复制
var p = new Proxy(target, {
  apply: function(target, thisArg, argumentsList) {
  }
});

参数

以下是传递给apply方法的参数,this上下文绑定在handler对象上.

target目标对象(函数)。

thisArg被调用时的上下文对象。

argumentsList被调用时的参数数组。

返回值

apply方法可以返回任何值。

描述

handler.apply?方法用于拦截函数的调用。

拦截

该方法会拦截目标对象的以下操作:

  • proxy(...args)
  • Function.prototype.apply() and Function.prototype.call()
  • Reflect.apply()

约束

无。

示例

以下代码演示如何捕获函数的调用。

代码语言:javascript
复制
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)); // "called: 1, 2, 3"
                         // 6

规范

Specification

Status

Comment

ECMAScript 2015 (6th Edition, ECMA-262)The definition of '[Call]' in that specification.

Standard

Initial definition.

ECMAScript 2017 Draft (ECMA-262)The definition of '[Call]' in that specification.

Draft

?

浏览器兼容性

Feature

Chrome

Firefox (Gecko)

Internet Explorer

Opera

Safari

Basic support

?

18 (18)

?

?

?

Feature

Android

Chrome for Android

Firefox Mobile (Gecko)

IE Mobile

Opera Mobile

Safari Mobile

Basic support

?

?

18.0 (18)

?

?

?

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com