Visit Mozilla.org

Core JavaScript 1.5 Reference:Global Objects:Object:propertyIsEnumerable

出典: MDC


目次

[編集] 概要

指定されたプロパティが列挙可能 (enumerable) かどうかを示す真偽値を返します。

[編集] 構文

propertyIsEnumerable(prop)

[編集] パラメータ

prop 
調べたいプロパティの名前。

[編集] 説明

Object の子孫にあたるあらゆるオブジェクトは propertyIsEnumerable メソッドを継承しています。このメソッドはあるオブジェクトのプロパティが for...in ループで列挙可能かどうかを特定することができます。もしオブジェクトが指定されたプロパティを持っていない場合、このメソッドは false を返します。for...in はオブジェクトのプロトタイプチェーンを考慮しますが、このメソッドはオブジェクトのプロトタイプチェーンを通じて継承されたプロパティには利きません。

[編集]

[編集] 例: 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 を返す

[編集] 例: 直接のプロパティと継承されたプロパティ

a = new Array();
a.propertyIsEnumerable('constructor');   // false を返す

function myConstructor() {
  this.isnot = 'not enumerable';
}

function secondConstructor() {
  this.is = 'is enumerable';
}

secondConstructor.prototype = new myConstructor;

o = new secondConstructor();
o.prop = 'is';

o.propertyIsEnumerable('is');      // true を返す
o.propertyIsEnumerable('prop');    // true を返す
o.propertyIsEnumerable('isnot');   // false を返す

[編集] 参照

for...in