JS Reflect.apply()

🌙
手机阅读
本文目录结构

静态方法 Reflect``.apply() 通过指定的参数列表发起对目标(target)函数的调用。

The source for this interactive example is stored in a GitHub repository. If you’d like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.

语法

Reflect.apply(target, thisArgument, argumentsList)

参数

target

目标函数。

thisArgument

target函数调用时绑定的this对象。

argumentsList

target函数调用时传入的实参列表,该参数应该是一个类数组的对象。

返回值

返回值是调用完带着指定参数和 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)Reflect.apply Standard 首次定义.
ECMAScript Latest Draft (ECMA-262)Reflect.apply Draft

相关连接

AXIHE / 精选资源

浏览全部教程

面试题

学习网站

前端培训
自己甄别

前端书籍

关于朱安邦

我叫 朱安邦,阿西河的站长,在杭州。

以前是一名平面设计师,后来开始接接触前端开发,主要研究前端技术中的JS方向。

业余时间我喜欢分享和交流自己的技术,欢迎大家关注我的 Bilibili

关注我: Github / 知乎

于2021年离开前端领域,目前重心放在研究区块链上面了

我叫朱安邦,阿西河的站长

目前在杭州从事区块链周边的开发工作,机械专业,以前从事平面设计工作。

2014年底脱产在老家自学6个月的前端技术,自学期间几乎从未出过家门,最终找到了满意的前端工作。更多>

于2021年离开前端领域,目前从事区块链方面工作了