这篇文章需要文法复核。如何帮忙。
handler.apply() 方法用于拦截函数的调用。
语法
var p = new Proxy(target, {
apply: function(target, thisArg, argumentsList) {
}
});
参数
以下是传递给apply方法的参数,this上下文绑定在handler对象上.
target- 目标对象(函数)。
thisArg- 被调用时的上下文对象。
argumentsList- 被调用时的参数数组。
返回值
apply方法可以返回任何值。
描述
handler.apply 方法用于拦截函数的调用。
拦截
该方法会拦截目标对象的以下操作:
proxy(...args)Function.prototype.apply()和Function.prototype.call()Reflect.apply()
约束
无。
示例
以下代码演示如何捕获函数的调用。
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) [[Call]] |
Standard | Initial definition. |
| ECMAScript 2017 Draft (ECMA-262) [[Call]] |
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) | ? | ? | ? |