Object.prototype.hasOwnProperty()
BaselineWidely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
hasOwnProperty()
はObject
インスタンスのメソッドで、オブジェクト自身が(継承されていない)指定されたプロパティを持っているかどうかを示す論理値を返します。
メモ:Object.hasOwn()
はhasOwnProperty()
よりもブラウザーの対応状況の面で推奨されます。
試してみましょう
const object1 = {};object1.property1 = 42;console.log(object1.hasOwnProperty("property1"));// Expected output: trueconsole.log(object1.hasOwnProperty("toString"));// Expected output: falseconsole.log(object1.hasOwnProperty("hasOwnProperty"));// Expected output: false
構文
hasOwnProperty(prop)
引数
返値
オブジェクトが指定したプロパティを自分自身で保有している場合はtrue
を返し、そうでない場合はfalse
を返します。
解説
hasOwnProperty()
メソッドは、指定したプロパティがオブジェクトの直接のプロパティである場合 (たとえその値がnull
またはundefined
であっても) 、true
を返します。プロパティが継承されているか、まったく宣言されていない場合はfalse
を返します。in
演算子とは異なり、このメソッドはオブジェクトのプロトタイプチェーンに指定したプロパティがあるかどうかを調べません。
なぜなら、ほとんどのオブジェクトはObject
の子孫であり、そのメソッドを継承しているからです。例えば配列 (Array
) はオブジェクトObject
なので、インデックスが存在するかどうかを調べるにはhasOwnProperty()
メソッドを使用することができます。
const fruits = ["Apple", "Banana", "Watermelon", "Orange"];fruits.hasOwnProperty(3); // true ('Orange')fruits.hasOwnProperty(4); // false - not defined
このメソッドは、再実装されたオブジェクトや、null
プロトタイプオブジェクト(Object.prototype
を継承していない)では利用できません。これらの場合の例は下記の通りです。
例
hasOwnProperty を使ってプロパティの存在を調べる
オブジェクトo
がprop
という名前のプロパティを持っているかどうかを特定する例を以下に示します。
const example = {};example.hasOwnProperty("prop"); // falseexample.prop = "exists";example.hasOwnProperty("prop"); // true - 'prop' が定義されているexample.prop = null;example.hasOwnProperty("prop"); // true - null 値を持つ独自プロパティexample.prop = undefined;example.hasOwnProperty("prop"); // true - undefined 値を持つ独自プロパティ
直接のプロパティと継承されたプロパティ
以下の例では、直接のプロパティとプロトタイプチェーンを通じて継承されたプロパティを区別します。
const example = {};example.prop = "exists";// `hasOwnProperty` は直接のプロパティについてのみ true を返すexample.hasOwnProperty("prop"); // trueexample.hasOwnProperty("toString"); // falseexample.hasOwnProperty("hasOwnProperty"); // false// 演算子 `in` は、直接または継承されたプロパティに対して true を返す"prop" in example; // true"toString" in example; // true"hasOwnProperty" in example; // true
オブジェクトのプロパティの反復処理
以下の例では、継承されたプロパティを除いてオブジェクトのプロパティを反復処理する方法を示します。
const buz = { fog: "stack",};for (const name in buz) { if (buz.hasOwnProperty(name)) { console.log(`this is fog (${name}) for sure. Value: ${buz[name]}`); } else { console.log(name); // toString or something else }}
なお、for...in
ループですでに列挙可能なアイテムのみが反復処理されるので、hasOwnProperty
自体は列挙可能なアイテムに厳密に限定されているため、ループ内に列挙できないプロパティが見られないことに基づいて想定するべきではありません (Object.getOwnPropertyNames()
のように)。
プロパティ名としての hasOwnProperty の使用
JavaScript はhasOwnProperty
というプロパティ名を保護していません。この名前を持ったプロパティを持つオブジェクトでは、正しくない結果が返る可能性があります。
const foo = { hasOwnProperty() { return false; }, bar: "Here be dragons",};foo.hasOwnProperty("bar"); // 再実装では常に false を返す
この問題を克服するために推奨される方法は、代わりにObject.hasOwn()
を使用することです(対応しているブラウザーで)。他にも、外部のhasOwnProperty
を使用する方法があります。
const foo = { bar: "Here be dragons" };// Use Object.hasOwn() method - recommendedObject.hasOwn(foo, "bar"); // true// Use the hasOwnProperty property from the Object prototypeObject.prototype.hasOwnProperty.call(foo, "bar"); // true// 別な Object の hasOwnProperty 使用して、// this を foo に設定して呼び出す({}).hasOwnProperty.call(foo, "bar"); // true
なお、後者の場合は新しくオブジェクトを生成しません。
Object.create(null) で作成されたオブジェクト
null
プロトタイプオブジェクトはObject.prototype
を継承していないので、hasOwnProperty()
はアクセス不可になります。
const foo = Object.create(null);foo.prop = "exists";foo.hasOwnProperty("prop"); // Uncaught TypeError: foo.hasOwnProperty は関数ではない
この場合の解決策は前の節と同じです。構成についてはObject.hasOwn()
を使用し、そうでなければ外部オブジェクトのhasOwnProperty()
を使用してください。
仕様書
Specification |
---|
ECMAScript® 2026 Language Specification # sec-object.prototype.hasownproperty |