A RetroSearch Logo

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

Search Query:

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

handler.apply() - JavaScript | MDN

handler.apply()

Baseline Widely available

La méthode handler.apply() représente une trappe pour un appel de fonctions.

Exemple interactif
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.

Valeur de retour

La méthode apply peut renvoyer n'importe quelle valeur.

Description

La méthode handler.apply est une trappe pour l'appel à une fonction.

Interceptions

Cette trappe intercepte les opérations suivantes :

Invariants

Si les invariants suivants ne sont pas respectés, le proxy lèvera une exception TypeError :

Exemples

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