Baseline Widely available
La méthode statique Reflect.apply()
permet d'appeler une fonction cible avec des arguments donnés.
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"
Syntaxe
Reflect.apply(cible, argumentThis, listeArguments);
Paramètres
cible
La fonction cible à appeler.
argumentThis
La valeur fournie pour this
lors de l'appel à cible
.
listeArguments
Un objet semblable à un tableau qui définit les arguments à passer à cible
. S'il vaut null
ou undefined
, aucun argument ne sera passé.
Le résultat de l'appel de la fonction cible indiquée avec la valeur this
et les arguments indiqués.
Une exception TypeError
, si cible ne peut pas être appelée.
Avec ES5, on utilise généralement Function.prototype.apply()
pour appeler une fonction avec une valeur this
donnée et des arguments donnés.
Function.prototype.apply.call(Math.floor, undefined, [1.75]);
Reflect.apply
permet de rendre cela plus concis et facile à comprendre.
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, "poneys", [3]);
// "e"
Spécifications Compatibilité des navigateurs Voir aussi
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