Object.hasOwn()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Object.hasOwn()
静的メソッドは、指定されたオブジェクトが、指定されたプロパティを自身のプロパティとして持っている場合に true
を返します。そのプロパティが継承されている場合、または存在しない場合、このメソッドは false
を返します。
メモ:Object.hasOwn()
静的メソッドは Object.hasOwnProperty()
インスタンスメソッドに代わるものとして意図されています。
試してみましょう
const object1 = { prop: "exists", }; console.log(Object.hasOwn(object1, "prop")); // Expected output: true console.log(Object.hasOwn(object1, "toString")); // Expected output: false console.log(Object.hasOwn(object1, "undeclaredPropertyValue")); // Expected output: false
構文
Object.hasOwn(obj, prop)
引数
返値
指定されたオブジェクトが指定されたプロパティを直接定義している場合、true
を返します。それ以外の場合は false
を返します。
解説
Object.hasOwn()
メソッドは、指定されたプロパティがオブジェクトの直接のプロパティである場合、そのプロパティ値が null
または undefined
であっても、true
を返します。プロパティが継承されているか、またはまったく宣言されていない場合、このメソッドは false
を返します。in
演算子とは異なり、このメソッドは、オブジェクトのプロトタイプチェーンで指定されたプロパティをチェックしません。
Object.prototype.hasOwnProperty()
よりも推奨される理由は、 null
プロトタイプオブジェクトや、継承した hasOwnProperty()
メソッドをオーバーライドしたオブジェクトに対して動作することです。これらの問題は、外部オブジェクトの Object.prototype.hasOwnProperty()
を呼び出すことで回避できますが、Object.hasOwn()
の方がより直感的に理解しやすいでしょう。
例
hasOwn を使ってプロパティの存在を調べる
次のコードは、example
オブジェクトに prop
という名前のプロパティが含まれているかどうかを判断する方法を示しています。
const example = {}; Object.hasOwn(example, "prop"); // false - 'prop' は定義されていない example.prop = "exists"; Object.hasOwn(example, "prop"); // true - 'prop' は定義されている example.prop = null; Object.hasOwn(example, "prop"); // true - null として定義されている example.prop = undefined; Object.hasOwn(example, "prop"); // true - undefined として定義されている
直接のプロパティと継承されたプロパティ
以下の例では、直接のプロパティとプロトタイプチェーンを通じて継承されたプロパティを区別します。
const example = {}; example.prop = "exists"; // `hasOwn` は直接のプロパティの場合のみ true を返す Object.hasOwn(example, "prop"); // returns true Object.hasOwn(example, "toString"); // returns false Object.hasOwn(example, "hasOwnProperty"); // returns false // `in` 演算子は直接または継承されたプロパティの場合に true を返す "prop" in example; // returns true "toString" in example; // returns true "hasOwnProperty" in example; // returns true
オブジェクトのプロパティの反復処理
オブジェクトの列挙可能なプロパティを反復処理するには、以下のようにします。
const example = { foo: true, bar: true }; for (const name of Object.keys(example)) { // … }
もし for...in
を使う必要がある場合には、Object.hasOwn()
を使うことで継承されたプロパティをスキップすることができます。
const example = { foo: true, bar: true }; for (const name in example) { if (Object.hasOwn(example, name)) { // … } }
配列のインデックスが存在するかどうかを調べる
Array
の要素は直接のプロパティとして定義されているので、hasOwn()
メソッドで特定のインデックスが存在するかどうかを調べることができます。
const fruits = ["Apple", "Banana", "Watermelon", "Orange"]; Object.hasOwn(fruits, 3); // true ('Orange') Object.hasOwn(fruits, 4); // false - not defined
hasOwnProperty の問題となるケース
このセクションでは、hasOwn()
が hasOwnProperty
に影響する問題から免れることを示します。まず、hasOwnProperty()
が再実装されたオブジェクトで使用することができます。
const foo = { hasOwnProperty() { return false; }, bar: "The dragons be out of office", }; if (Object.hasOwn(foo, "bar")) { console.log(foo.bar); // true - hasOwnProperty() が再実装されていても結果に影響しない }
また、 null
プロトタイプオブジェクトでも使用することができます。これらは Object.prototype
を継承していないため、hasOwnProperty()
はアクセスできません。
const foo = Object.create(null); foo.prop = "exists"; if (Object.hasOwn(foo, "prop")) { console.log(foo.prop); //true - オブジェクトの作成方法に関係なく動作する }
仕様書
Specification |
---|
ECMAScript® 2026 Language Specification # sec-object.hasown |