Reflect.apply()

O método estático Reflect.apply() chama uma função alvo com os argumentos especificados.

Sintaxe

Reflect.apply(target, thisArgument, argumentsList)

Parâmetros

target

Função que será chamada.

thisArgument

O valor de "this" que será usado pela function do target.

argumentsList

Um objeto do tipo array que especifica os argumentos com que o target deve ser chamado.

Valor de retorno

O resultado da função alvo chamada com o this e argumentos especificados.

Exceções

Um TypeError, se a função especificada no target não for invocável.

Descrição

No ES5, tipicamente é usado o método Function.prototype.apply() para chamar uma função com o valor de this e argumentos fornecidos como um array (ou um array-like object).

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

Com o Reflect.apply isso se torna menos verboso e mais fácil de entender.

Exemplos

Usando 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, /ab/, ["confabulation"]).index;
// 4

Reflect.apply("".charAt, "ponies", [3]);
// "i"

Especificações

Specification
ECMAScript Language Specification
# sec-reflect.apply

Compatibilidade com navegadores

BCD tables only load in the browser

Veja também