Object.prototype.hasOwnProperty()
hasOwnProperty()
メソッドは、オブジェクト自身が(継承されていない)指定されたプロパティを持っているかどうかを示す真偽値を返します。
試してみましょう
構文
obj.hasOwnProperty(prop)
引数
返値
Boolean
で、オブジェクトが指定されたプロパティを自身のプロパティとして持っているかどうかを示す
解説
注
hasOwnProperty
はプロパティの値が null
または undefined
であっても true を返します。
o = new Object();
o.propOne = null;
o.hasOwnProperty('propOne'); // true を返す
o.propTwo = undefined;
o.hasOwnProperty('propTwo'); // true を返す
例
hasOwnProperty を使ってプロパティの存在を調べる
オブジェクト o
が prop
という名前のプロパティを持っているかどうかを特定する例を以下に示します。
o = new Object();
o.hasOwnProperty('prop'); // false を返す
o.prop = 'exists';
o.hasOwnProperty('prop'); // true を返す
直接のプロパティと継承されたプロパティ
以下の例では、直接のプロパティとプロトタイプチェーンを通じて継承されたプロパティを区別します。
o = new Object();
o.prop = 'exists';
o.hasOwnProperty('prop'); // true を返す
o.hasOwnProperty('toString'); // false を返す
o.hasOwnProperty('hasOwnProperty'); // false を返す
オブジェクトのプロパティの反復処理
以下の例では、継承されたプロパティを除いてオブジェクトのプロパティを反復処理する方法を示します。なお、 for...in
ループですでに列挙可能なアイテムのみが反復処理されるので、 hasOwnProperty
自体は列挙可能なアイテムに厳密に限定されているため、ループ内に列挙できないプロパティが見られないことに基づいて想定するべきではありません (Object.getOwnPropertyNames()
のように)。
var buz = {
fog: 'stack'
};
for (var 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
}
}
プロパティ名としての hasOwnProperty の使用
JavaScript は hasOwnProperty
というプロパティ名を保護していません。したがって、オブジェクトがこの名前のプロパティを持っている場合が存在すれば、正しい結果を得るためには外部の hasOwnProperty
を使用する必要があります。
var foo = {
hasOwnProperty: function() {
return false;
},
bar: 'Here be dragons'
};
foo.hasOwnProperty('bar'); // 常に false を返す
// 別な Object の hasOwnProperty 使用して、
// this を foo に設定して呼び出す
({}).hasOwnProperty.call(foo, 'bar'); // true
// この目的では、 Object プロトタイプの hasOwnProperty プロパティを
// 使用することもできます。
Object.prototype.hasOwnProperty.call(foo, 'bar'); // true
なお、後者の場合は新しくオブジェクトを生成しません。
仕様書
Specification |
---|
ECMAScript Language Specification # sec-object.prototype.hasownproperty |
ブラウザーの互換性
BCD tables only load in the browser