A RetroSearch Logo

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

Search Query:

Showing content from https://developer.cdn.mozilla.net/fr/docs/Web/JavaScript/Reference/Global_Objects/Proxy/Proxy/has below:

handler.has() - JavaScript | MDN

handler.has()

Baseline Widely available

La méthode handler.has() est une trappe pour l'opérateur in.

Exemple interactif
const handler1 = {
  has(target, key) {
    if (key[0] === "_") {
      return false;
    }
    return key in target;
  },
};

const monster1 = {
  _secret: "easily scared",
  eyeCount: 4,
};

const proxy1 = new Proxy(monster1, handler1);
console.log("eyeCount" in proxy1);
// Expected output: true

console.log("_secret" in proxy1);
// Expected output: false

console.log("_secret" in monster1);
// Expected output: true
Syntaxe
var p = new Proxy(cible, {
  has: function (cible, prop) {},
});
Paramètres

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

cible

L'objet cible.

prop

Le nom ou le symbole (Symbol) de la propriété dont on veut connaître l'existence.

Valeur de retour

La méthode has doit renvoyer une valeur booléenne.

Description

La méthode handler.has est une trappe pour l'opérateur in.

Interceptions

Cette trappe permet d'intercepter 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 qui suit, on intercepte l'opérateur in :

var p = new Proxy(
  {},
  {
    has: function (cible, prop) {
      console.log("appelée : " + prop);
      return true;
    },
  },
);

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

L'exemple suivant ne respecte pas un invariant :

var obj = { a: 10 };
Object.preventExtensions(obj);
var p = new Proxy(obj, {
  has: function (cible, prop) {
    return false;
  },
});

"a" in p; // 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