→
指定されたプロパティが列挙可能で、オブジェクト自身のプロパティであることを示す Boolean を返します。
obj | 調べられるオブジェクト。 |
---|---|
prop | 調べたいプロパティの名前。 |
指定されたプロパティが列挙可能であり、かつオブジェクト自体のプロパティであるかどうかを示す Boolean。
すべてのオブジェクトは propertyIsEnumerable メソッドを持っています。このメソッドはあるオブジェクトのプロパティが、プロトタイプ・チェーンを通じて継承されたプロパティを除いて for...in ループで列挙可能かどうかを特定することができます。もしオブジェクトが指定されたプロパティを持っていない場合、このメソッドは false を返します。
以下の例はオブジェクトと配列での propertyIsEnumerable の使い方を示しています:
var o = {}; o.prop = 'is enumerable'; D=o.propertyIsEnumerable('prop'); //=true var a = []; a[0] = 'is enumerable'; D=a.propertyIsEnumerable(0); //=true
以下の例はユーザー定義プロパティと組み込みプロパティの列挙可能性を実証しています:
var a = ['is enumerable']; D=a.propertyIsEnumerable(0); //=true D=a.propertyIsEnumerable('length'); //=false
D=Math.propertyIsEnumerable('random'); //=false
D=this.propertyIsEnumerable('Math'); //=false
var a = []; D=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'; D=o.propertyIsEnumerable('arbitraryProperty'); //=true D=o.propertyIsEnumerable('method'); //=true D=o.propertyIsEnumerable('property'); //=false o.property = 'is enumerable'; D=o.propertyIsEnumerable('property'); //=true // これらはすべて false を返します。これは、 (最後の2つは for-in で // 反復処理可能であるにもかかわらず) propertyIsEnumerable が考慮しない // プロトタイプであるためです。 D=o.propertyIsEnumerable('prototype'); //=false。(as of JS 1.8.1/FF3.6) D=o.propertyIsEnumerable('constructor'); //=false D=o.propertyIsEnumerable('firstMethod'); //=false