--- title: Object.prototype.propertyIsEnumerable() slug: Web/JavaScript/Reference/Global_Objects/Object/propertyIsEnumerable tags: - JavaScript - Method - Object - Prototype - プロトタイプ - メソッド translation_of: Web/JavaScript/Reference/Global_Objects/Object/propertyIsEnumerable ---
propertyIsEnumerable() メソッドは、指定されたプロパティが列挙可能で、オブジェクト自身のプロパティであることを示す Boolean を返します。
obj.propertyIsEnumerable(prop)
prop指定されたプロパティが列挙可能であり、かつオブジェクト自体のプロパティであるかどうかを示す {{jsxref("Boolean")}} 。
すべてのオブジェクトは propertyIsEnumerable メソッドを持っています。このメソッドはあるオブジェクトのプロパティが、プロトタイプチェーンを通じて継承されたプロパティを除いて {{jsxref("Statements/for...in", "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 を返す
| 仕様書 |
|---|
| {{SpecName('ESDraft', '#sec-object.prototype.propertyisenumerable', 'Object.prototype.propertyIsEnumerable')}} |
{{Compat("javascript.builtins.Object.propertyIsEnumerable")}}