Baseline Widely available
La méthode handler.apply()
représente une trappe pour un appel de fonctions.
function sum(a, b) {
return a + b;
}
const handler = {
apply: function (target, thisArg, argumentsList) {
console.log(`Calculate sum: ${argumentsList}`);
// Expected output: "Calculate sum: 1,2"
return target(argumentsList[0], argumentsList[1]) * 10;
},
};
const proxy1 = new Proxy(sum, handler);
console.log(sum(1, 2));
// Expected output: 3
console.log(proxy1(1, 2));
// Expected output: 30
Syntaxe
var p = new Proxy(cible, {
apply: function (cible, thisArg, listeArguments) {},
});
Paramètres
Les paramètres suivants sont passés à la méthode apply
. Ici, this
est lié au gestionnaire.
cible
L'objet cible.
thisArg
L'argument this
pour cet appel.
listeArguments
La liste d'arguments pour l'appel.
La méthode apply
peut renvoyer n'importe quelle valeur.
La méthode handler.apply
est une trappe pour l'appel à une fonction.
Cette trappe intercepte les opérations suivantes :
proxy(...args)
Function.prototype.apply()
et Function.prototype.call()
Reflect.apply()
Si les invariants suivants ne sont pas respectés, le proxy lèvera une exception TypeError
:
Dans l'exemple ci-dessous, on piège un appel de fonction.
var p = new Proxy(function () {}, {
apply: function (target, thisArg, argumentsList) {
console.log("called: " + argumentsList.join(", "));
return argumentsList[0] + argumentsList[1] + argumentsList[2];
},
});
console.log(p(1, 2, 3)); // "called: 1, 2, 3"
// 6
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