Object.prototype.propertyIsEnumerable()
propertyIsEnumerable()
メソッドは、指定されたプロパティが列挙可能で、オブジェクト自身のプロパティであることを示す Boolean を返します。
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
構文
obj.propertyIsEnumerable(prop)
引数
prop
- 調べたいプロパティの名前。
返値
指定されたプロパティが列挙可能であり、かつオブジェクト自体のプロパティであるかどうかを示す Boolean
。
解説
すべてのオブジェクトは propertyIsEnumerable
メソッドを持っています。このメソッドはあるオブジェクトのプロパティが、プロトタイプチェーンを通じて継承されたプロパティを除いて for...in
ループで列挙可能かどうかを特定することができます。もしオブジェクトが指定されたプロパティを持っていない場合、このメソッドは false
を返します。
例
propertyIsEnumerable
の基本的な使い方
以下の例はオブジェクトと配列での propertyIsEnumerable
の使い方を示しています。
var o = {};
var a = [];
o.prop = 'is enumerable';
a[0] = 'is enumerable';
o.propertyIsEnumerable('prop'); // true を返す
a.propertyIsEnumerable(0); // true を返す
ユーザー定義オブジェクトと組み込みオブジェクト
以下の例はユーザー定義プロパティと組み込みプロパティの列挙可能性を実証しています。
var a = ['is enumerable'];
a.propertyIsEnumerable(0); // true を返す
a.propertyIsEnumerable('length'); // false を返す
Math.propertyIsEnumerable('random'); // false を返す
this.propertyIsEnumerable('Math'); // false を返す
直接のプロパティと継承されたプロパティ
var a = [];
a.propertyIsEnumerable('constructor'); // false を返す
function firstConstructor() {
this.property = 'is not enumerable';
}
firstConstructor.prototype.firstMethod = function() {};
function secondConstructor() {
this.method = function method() { return 'is enumerable'; };
}
secondConstructor.prototype = new firstConstructor;
secondConstructor.prototype.constructor = secondConstructor;
var o = new secondConstructor();
o.arbitraryProperty = 'is enumerable';
o.propertyIsEnumerable('arbitraryProperty'); // true を返す
o.propertyIsEnumerable('method'); // true を返す
o.propertyIsEnumerable('property'); // false を返す
o.property = 'is enumerable';
o.propertyIsEnumerable('property'); // true を返す
// これらはすべて false を返します。これは、 (最後の2つは for-in で
// 反復処理可能であるにもかかわらず) propertyIsEnumerable が考慮しない
// プロトタイプであるためです。
o.propertyIsEnumerable('prototype'); // false を返す (as of JS 1.8.1/FF3.6)
o.propertyIsEnumerable('constructor'); // false を返す
o.propertyIsEnumerable('firstMethod'); // false を返す
仕様書
ブラウザーの互換性
BCD tables only load in the browser