diff options
Diffstat (limited to 'files/zh-cn/web/javascript/reference/global_objects/reflect/apply/index.html')
-rw-r--r-- | files/zh-cn/web/javascript/reference/global_objects/reflect/apply/index.html | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/global_objects/reflect/apply/index.html b/files/zh-cn/web/javascript/reference/global_objects/reflect/apply/index.html new file mode 100644 index 0000000000..d3cb19ec78 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/reflect/apply/index.html @@ -0,0 +1,101 @@ +--- +title: Reflect.apply() +slug: Web/JavaScript/Reference/Global_Objects/Reflect/apply +tags: + - JavaScript + - Method + - Reference + - Reflect +translation_of: Web/JavaScript/Reference/Global_Objects/Reflect/apply +--- +<div>{{JSRef}}</div> + +<p>静态方法 <code><strong>Reflect</strong></code><strong><code>.apply()</code></strong> 通过指定的参数列表发起对目标(target)函数的调用。</p> + +<div>{{EmbedInteractiveExample("pages/js/reflect-apply.html")}}</div> + + + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox">Reflect.apply(target, thisArgument, argumentsList) +</pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt>target</dt> + <dd>目标函数。</dd> + <dt>thisArgument</dt> + <dd>target函数调用时绑定的this对象。</dd> + <dt>argumentsList</dt> + <dd>target函数调用时传入的实参列表,该参数应该是一个类数组的对象。</dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<p>返回值是调用完带着指定参数和 <code>this</code> 值的给定的函数后返回的结果。</p> + +<h3 id="异常">异常</h3> + +<p>如果 <code>target</code> 对象不可调用,抛出 {{jsxref("TypeError")}}。</p> + +<h2 id="描述">描述</h2> + +<p>该方法与ES5中{{jsxref("Function.prototype.apply()")}}方法类似:调用一个方法并且显式地指定 <code>this</code> 变量和参数列表(arguments) ,参数列表可以是数组,或类似数组的对象。</p> + +<pre class="brush: js">Function.prototype.apply.call(Math.floor, undefined, [1.75]);</pre> + +<p>使用 <code>Reflect.apply</code> 方法会使代码更加简洁易懂。</p> + +<h2 id="使用示例">使用示例</h2> + +<h3 id="Reflect.apply"><code>Reflect.apply()</code></h3> + +<pre class="brush: js">Reflect.apply(Math.floor, undefined, [1.75]); +// 1; + +Reflect.apply(String.fromCharCode, undefined, [104, 101, 108, 108, 111]); +// "hello" + +Reflect.apply(RegExp.prototype.exec, /ab/, ["confabulation"]).index; +// 4 + +Reflect.apply("".charAt, "ponies", [3]); +// "i" +</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-reflect.apply', 'Reflect.apply')}}</td> + <td>{{Spec2('ES6')}}</td> + <td>首次定义.</td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-reflect.apply', 'Reflect.apply')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td></td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + + + +<p>{{Compat("javascript.builtins.Reflect.apply")}}</p> + +<h2 id="相关连接">相关连接</h2> + +<ul> + <li>{{jsxref("Reflect")}}</li> + <li>{{jsxref("Function.prototype.apply()")}}</li> +</ul> |