A RetroSearch Logo

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

Search Query:

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

handler.has() - JavaScript | MDN

handler.has()

Baseline Widely available

handler.has() は、オブジェクトの [[HasProperty]] 内部メソッドに対するトラップです。in 演算子などの操作で使用されます。

試してみましょう
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
構文
new Proxy(target, {
  has(target, prop) {
  }
});
引数

次の引数は has() メソッドに渡されます。 this はハンドラーにバインドされます。

target

ターゲットオブジェクトです。

prop

存在を確認するプロパティ名です。

返値

has メソッドは論理値を返さなければなりません。

解説 介入

このトラップは下記の操作に介入できます。

他にも、[[HasProperty]] 内部メソッドを呼び出すあらゆる操作に介入できます。

不変条件

以下の不変条件に違反している場合、プロキシーは TypeError を発生します。

例 in 演算子のトラップ

次のコードでは in 演算子をトラップします。

const p = new Proxy(
  {},
  {
    has(target, prop) {
      console.log(`called: ${prop}`);
      return true;
    },
  },
);

console.log("a" in p);
// "called: a"
// true

次のコードでは不変条件に違反します。

const obj = { a: 10 };
Object.preventExtensions(obj);

const p = new Proxy(obj, {
  has(target, prop) {
    return false;
  },
});

"a" in p; // TypeError is thrown
仕様書 ブラウザーの互換性 関連情報

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