JavaScript参考手册
Reflect
Reflect.apply
静态方法 Reflect.apply()
通过指定的参数列表发起对目标(target)函数的调用。
语法
Reflect.apply(target, thisArgument, argumentsList)
参数
target目标函数。thisArgumenttarget函数调用时绑定的this对象。argumentsListtarget函数调用时传入的实参列表,该参数应该是一个类数组的对象。
返回值
使用指定的this
值和参数调用给定的目标函数的结果。
异常
如果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)The definition of 'Reflect.apply' in that specification. |
Standard |
Initial definition. |
ECMAScript Latest Draft (ECMA-262)The definition of 'Reflect.apply' in that specification. |
Draft |
|
浏览器兼容性
Feature |
Chrome |
Edge |
Firefox (Gecko) |
Internet Explorer |
Opera |
Safari |
---|---|---|---|---|---|---|
Basic support |
49 |
12 |
42 (42) |
No support |
36 |
10 |
Feature |
Android |
Chrome for Android |
Edge |
Firefox Mobile (Gecko) |
IE Mobile |
Opera Mobile |
Safari Mobile |
---|---|---|---|---|---|---|---|
Basic support |
No support |
49 |
(Yes) |
42.0 (42) |
No support |
No support |
10 |
Reflect相关

JavaScript 是一种高级编程语言,通过解释执行,是一门动态类型,面向对象(基于原型)的解释型语言。它已经由ECMA(欧洲电脑制造商协会)通过 ECMAScript 实现语言的标准化。它被世界上的绝大多数网站所使用,也被世界主流浏览器( Chrome、IE、FireFox、Safari、Opera )支持。JavaScript 是一门基于原型、函数先行的语言,是一门多范式的语言,它支持面向对象编程,命令式编程,以及函数式编程。它提供语法来操控文本、数组、日期以及正则表达式等,不支持 I/O,比如网络