このページはコミュニティーの尽力で英語から翻訳されました。MDN Web Docs コミュニティーについてもっと知り、仲間になるにはこちらから。

View in English Always switch to English

Reflect.has()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2016年9月.

Reflect.has() は静的メソッドで、in 演算子と似ていますが、関数として存在します。

試してみましょう

const object = {
  property1: 42,
};

console.log(Reflect.has(object, "property1"));
// 予想される結果: true

console.log(Reflect.has(object, "property2"));
// 予想される結果: false

console.log(Reflect.has(object, "toString"));
// 予想される結果: true

構文

js
Reflect.has(target, propertyKey)

引数

target

プロパティを探す対象のオブジェクト。

propertyKey

チェックするプロパティ名。

返値

対象がプロパティを持つかどうかを示す論理値 (Boolean)。

例外

TypeError

target がオブジェクトではない場合に発生します。

解説

Reflect.has() は、オブジェクトにプロパティが存在するかどうかを調べる反射的意味づけを提供します。つまり、Reflect.has(target, propertyKey) は意味づけ的に次のものと同等です。

js
propertyKey in target;

Reflect.has() は、target[[HasProperty]] オブジェクト内部メソッド を呼び出します。

Reflect.has() の使用

js
Reflect.has({ x: 0 }, "x"); // true
Reflect.has({ x: 0 }, "y"); // false

// プロトタイプチェーンのプロパティがあるため、true が返る
Reflect.has({ x: 0 }, "toString");

// Proxy with .has() handler method
obj = new Proxy(
  {},
  {
    has(t, k) {
      return k.startsWith("door");
    },
  },
);
Reflect.has(obj, "doorbell"); // true
Reflect.has(obj, "dormitory"); // false

Reflect.has は継承されたプロパティについて true を返し、これは in 演算子と同様です。

js
const a = { foo: 123 };
const b = { __proto__: a };
const c = { __proto__: b };
// The prototype chain is: c -> b -> a
Reflect.has(c, "foo"); // true

仕様書

Specification
ECMAScript® 2026 Language Specification
# sec-reflect.has

ブラウザーの互換性

関連情報