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 September 2016.
静的な Reflect.has()
メソッドは、機能としては in
演算子のように動作します。
試してみましょう
const object1 = {
property1: 42,
};
console.log(Reflect.has(object1, "property1"));
// Expected output: true
console.log(Reflect.has(object1, "property2"));
// Expected output: false
console.log(Reflect.has(object1, "toString"));
// Expected output: true
構文
Reflect.has(target, propertyKey)
引数
target
-
プロパティを探す対象のオブジェクト。
propertyKey
-
チェックするプロパティ名。
返値
対象がプロパティを持つかどうかを示す Boolean
値。
例外
解説
Reflect.has
メソッドは、オブジェクトプロパティがあるかをチェックします。機能としては in
演算子のように動作します。
例
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® 2025 Language Specification # sec-reflect.has |
ブラウザーの互換性
Report problems with this compatibility data on GitHubdesktop | mobile | server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
has |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.