1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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>
|