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/get below:

handler.get() - JavaScript | MDN

handler.get()

Baseline Widely available

La méthode handler.get() est une trappe pour intercepter l'accès à la valeur d'une propriété.

Exemple interactif
const monster1 = {
  secret: "easily scared",
  eyeCount: 4,
};

const handler1 = {
  get: function (target, prop, receiver) {
    if (prop === "secret") {
      return `${target.secret.substring(0, 4)} ... shhhh!`;
    }
    return Reflect.get(...arguments);
  },
};

const proxy1 = new Proxy(monster1, handler1);

console.log(proxy1.eyeCount);
// Expected output: 4

console.log(proxy1.secret);
// Expected output: "easi ... shhhh!"
Syntaxe
var p = new Proxy(cible, {
  get: function (cible, propriété, récepteur) {},
});
Paramètres

Les paramètres suivants sont passés à la méthode get. this est lié au gestionnaire.

cible

L'objet cible.

propriété

Le nom ou le symbole (Symbol) de la propriété qu'on souhaite obtenir.

récepteur

Le proxy ou un objet qui hérite du proxy.

Valeur de retour

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

Description

La méthode handler.get est une trappe pour intercepter l'accès à une propriété.

Interceptions

Cette trappe permet d'intercepter les opérations suivantes :

Invariants

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

Exemples

Dans l'exemple suivant, on intercepte les accès aux propriétés :

var p = new Proxy(
  {},
  {
    get: function (cible, propriété, récepteur) {
      console.log("appelée : " + propriété);
      return 10;
    },
  },
);

console.log(p.a); // "appelée : a"
// 10

Le code suivant ne respecte pas l'invariant :

var obj = {};
Object.defineProperty(obj, "a", {
  configurable: false,
  enumerable: false,
  value: 10,
  writable: false,
});

var p = new Proxy(obj, {
  get: function (cible, propriété) {
    return 20;
  },
});

p.a; // exception TypeError levé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