Reflect.apply()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2016.

Статический метод 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

Значение переменной this во время вызова функции target.

argumentsList

Объект, подобный массиву, содержащий аргументы, с которыми должна быть вызвана функция target.

Возвращаемое значение

Возвращается значение, которое вернёт функция target с указанным значением переменной this и аргументами.

Исключения

TypeError, если функция target не может быть вызвана.

Описание

В ES5, обычно используется метод Function.prototype.apply(), чтобы вызвать функцию с указанным значением переменной this и arguments, переданными как массив (или массивоподобный объект).

js
Function.prototype.apply.call(Math.floor, undefined, [1.75]);

С использованием Reflect.apply это действие становится более понятным и занимает меньше места в коде.

Примеры

Использование Reflect.apply()

js
Reflect.apply(Math.floor, undefined, [1.75]);
// 1;

Reflect.apply(String.fromCharCode, undefined, [104, 101, 108, 108, 111]);
// "hello"

Reflect.apply(RegExp.prototype.exec, /вы/, [
  "превысокомногорассмотрительствующий",
]).index;
// 4

Reflect.apply("".charAt, "пони", [3]);
// "и"

Спецификации

Specification
ECMAScript® 2025 Language Specification
# sec-reflect.apply

Совместимость с браузерами

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
apply

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

Смотрите также