A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Reflect/apply below:

Reflect.apply() - JavaScript | MDN

Reflect.apply()

Baseline Widely available

Статический метод 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, переданными как массив (или массивоподобный объект).

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, /вы/, [
  "превысокомногорассмотрительствующий",
]).index;
// 4

Reflect.apply("".charAt, "пони", [3]);
// "и"
Спецификации Совместимость с браузерами Смотрите также

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4