静态方法 Reflect
.apply()
通过指定的参数列表发起对目标(target)函数的调用。
Reflect.apply(target, thisArgument, argumentsList)
如果target对象不可调用,抛出TypeError
。
该方法与ES5中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 |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262) Reflect.apply |
Standard | 首次定义. |
ECMAScript Latest Draft (ECMA-262) Reflect.apply |
Draft |
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | 49 | 42 (42) | 未实现 | 未实现 | 未实现 |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | 未实现 | 未实现 | 42.0 (42) | 未实现 | 未实现 | 未实现 |