概要
指定されたプロパティが列挙可能 (enumerable) かどうかを示す真偽値を返します。
構文
propertyIsEnumerable(prop)
パラメータ
prop- 調べたいプロパティの名前。
説明
Object の子孫にあたるあらゆるオブジェクトは propertyIsEnumerable メソッドを継承しています。このメソッドはあるオブジェクトのプロパティが for...in ループで列挙可能かどうかを特定することができます。もしオブジェクトが指定されたプロパティを持っていない場合、このメソッドは false を返します。for...in はオブジェクトのプロトタイプチェーンを考慮しますが、このメソッドはオブジェクトのプロトタイプチェーンを通じて継承されたプロパティには利きません。
注記: JavaScript 1.8.1 (Firefox 3.6) より、
propertyIsEnumerable("prototype") は true でなく false を返します。これは ECMAScript 5 に準じるものです。例
例: propertyIsEnumerable の基本的な使い方
以下の例はオブジェクトと配列での propertyIsEnumerable の使い方を示しています。
o = new Object();
a = new Array();
o.prop = 'is enumerable';
a[0] = 'is enumerable';
o.propertyIsEnumerable('prop'); // true を返す
a.propertyIsEnumerable(0); // true を返す
例: ユーザー定義オブジェクトと組み込みオブジェクト
以下の例はユーザー定義プロパティと組み込みプロパティの列挙可能性を実証しています。
a = new Array('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 を返す
/* これらは propertyIsEnumerable が考慮しないプロトタイプであるため、
(※最後の 2 つはイテレート可能であるにも関わらず)全て false を返します */
o.propertyIsEnumerable('prototype'); // false を返す (as of JS 1.8.1/FF3.6)
o.propertyIsEnumerable('constructor'); // false を返す
o.propertyIsEnumerable('firstMethod'); // false を返す