Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1134897 of handler.apply()

  • 版本网址缩略名: Web/JavaScript/Reference/Global_Objects/Proxy/handler/apply
  • 版本标题: handler.apply()
  • 版本 id: 1134897
  • 创建于:
  • 创建者: wtZZx
  • 是否是当前版本?
  • 评论

修订内容

{{JSRef}}

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

语法

var p = new Proxy(target, {
  apply: function(target, thisArg, argumentsList) {
  }
});

参数

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

target
目标对象(函数)。
thisArg
被调用时的上下文对象。
argumentsList
被调用时的参数数组。

返回值

apply方法可以返回任何值。

描述

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

拦截

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

  • proxy(...args)
  • {{jsxref("Function.prototype.apply()")}} 和 {{jsxref("Function.prototype.call()")}}
  • {{jsxref("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
{{SpecName('ES6', '#sec-proxy-object-internal-methods-and-internal-slots-call-thisargument-argumentslist', '[[Call]]')}} {{Spec2('ES6')}} Initial definition.
{{SpecName('ESDraft', '#sec-proxy-object-internal-methods-and-internal-slots-call-thisargument-argumentslist', '[[Call]]')}} {{Spec2('ESDraft')}}  

浏览器兼容性

{{CompatibilityTable}}
Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{CompatUnknown}} {{CompatGeckoDesktop("18")}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatUnknown}} {{CompatUnknown}} {{CompatGeckoMobile("18")}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}

另见

  • {{jsxref("Proxy")}}
  • {{jsxref("Proxy.handler", "handler")}}
  • {{jsxref("Function.prototype.apply")}}
  • {{jsxref("Function.prototype.call")}}
  • {{jsxref("Reflect.apply()")}}

修订版来源

<div>{{JSRef}}</div>

<p><strong><code>handler.apply()</code></strong>&nbsp;方法用于拦截函数的调用。</p>

<h2 id="语法">语法</h2>

<pre class="brush: js">
var p = new Proxy(target, {
  apply: function(target, thisArg, argumentsList) {
  }
});
</pre>

<h3 id="参数">参数</h3>

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

<dl>
 <dt><code>target</code></dt>
 <dd>目标对象(函数)。</dd>
 <dt><code>thisArg</code></dt>
 <dd>被调用时的上下文对象。</dd>
 <dt><code>argumentsList</code></dt>
 <dd>被调用时的参数数组。</dd>
</dl>

<h3 id="返回值">返回值</h3>

<p><code>apply方法可以返回任何值。</code></p>

<h2 id="描述">描述</h2>

<p><strong><code>handler.apply</code></strong>&nbsp;方法用于拦截函数的调用。</p>

<h3 id="拦截">拦截</h3>

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

<ul>
 <li><code>proxy(...args)</code></li>
 <li>{{jsxref("Function.prototype.apply()")}} 和 {{jsxref("Function.prototype.call()")}}</li>
 <li>{{jsxref("Reflect.apply()")}}</li>
</ul>

<h3 id="不变量">约束</h3>

<p>无。</p>

<h2 id="示例">示例</h2>

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

<pre class="brush: js">
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
</pre>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-proxy-object-internal-methods-and-internal-slots-call-thisargument-argumentslist', '[[Call]]')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>Initial definition.</td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-proxy-object-internal-methods-and-internal-slots-call-thisargument-argumentslist', '[[Call]]')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td>&nbsp;</td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>

<div>{{CompatibilityTable}}</div>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatGeckoDesktop("18")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Chrome for Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatGeckoMobile("18")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="另见">另见</h2>

<ul>
 <li>{{jsxref("Proxy")}}</li>
 <li>{{jsxref("Proxy.handler", "handler")}}</li>
 <li>{{jsxref("Function.prototype.apply")}}</li>
 <li>{{jsxref("Function.prototype.call")}}</li>
 <li>{{jsxref("Reflect.apply()")}}</li>
</ul>
恢复到这个版本