大网数据广告活动
Web 标准与前端开发

Reflect.apply()

· 52CE 我爱测

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

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

JavaScript Demo: Reflect.apply()

console.log(Reflect.apply(Math.floor, undefined, [1.75]));
// Expected output: 1

console.log(
  Reflect.apply(String.fromCharCode, undefined, [104, 101, 108, 108, 111]),
);
// Expected output: "hello"

console.log(
  Reflect.apply(RegExp.prototype.exec, /ab/, ["confabulation"]).index,
);
// Expected output: 4

console.log(Reflect.apply("".charAt, "ponies", [3]));
// Expected output: "i"

语法

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"

规范

浏览器兼容性

相关连接

  • Reflect
  • Function.prototype.apply()

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。