--- 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 ---
静态方法 Reflect.apply() 通过指定的参数列表发起对目标(target)函数的调用。
Reflect.apply(target, thisArgument, argumentsList)
返回值是调用完带着指定参数和 this 值的给定的函数后返回的结果。
如果 target 对象不可调用,抛出 {{jsxref("TypeError")}}。
该方法与ES5中{{jsxref("Function.prototype.apply()")}}方法类似:调用一个方法并且显式地指定 this 变量和参数列表(arguments) ,参数列表可以是数组,或类似数组的对象。
Function.prototype.apply.call(Math.floor, undefined, [1.75]);
使用 Reflect.apply 方法会使代码更加简洁易懂。
Reflect.apply()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"
| Specification | Status | Comment |
|---|---|---|
| {{SpecName('ES6', '#sec-reflect.apply', 'Reflect.apply')}} | {{Spec2('ES6')}} | 首次定义. |
| {{SpecName('ESDraft', '#sec-reflect.apply', 'Reflect.apply')}} | {{Spec2('ESDraft')}} |
{{Compat("javascript.builtins.Reflect.apply")}}