概要
与えられたオブジェクトで発見されたすべてのプロパティ (列挙可能・不可能を問わず) の配列を返します。
構文
Object.getOwnPropertyNames(obj)
引数
- obj
- 自身の列挙可能および列挙不可能なプロパティが返されるオブジェクトです。
説明
Object.getOwnPropertyNames は、obj で発見された列挙可能および列挙不可能なプロパティに対応する文字列を要素とする配列を返します。列挙可能なプロパティの配列内での順序は、オブジェクトで for...in ループ (または Object.keys) を実行して判明するものの順序と同じです。列挙不可能なプロパティの配列内での順序および列挙可能なプロパティとの並び方は定義されていません。
例
Object.getOwnPropertyNames() の使用
var arr = ['a', 'b', 'c'];
console.log(Object.getOwnPropertyNames(arr).sort());
// ["0", "1", "2", "length"] と出力されます
// Array-like object
var obj = { 0: 'a', 1: 'b', 2: 'c' };
console.log(Object.getOwnPropertyNames(obj).sort());
// ["0", "1", "2"] と出力されます
// Array.forEach を使ったプロパティと値の出力
Object.getOwnPropertyNames(obj).forEach(function(val, idx, array) {
console.log(val + ' -> ' + obj[val]);
});
// 出力結果
// 0 -> a
// 1 -> b
// 2 -> c
// 列挙不可能なプロパティ
var my_obj = Object.create({}, {
getFoo: {
value: function() { return this.foo; },
enumerable: false
}
});
my_obj.foo = 1;
console.log(Object.getOwnPropertyNames(my_obj).sort());
// ["foo", "getFoo"] と出力されます
列挙可能なプロパティのみ取得したい場合は Object.keys を参照するか、for...in ループ を用いてください。 (for...in ループでは hasOwnProperty() でフィルタリングされない限りは、そのオブジェクト上で直接見つかる列挙可能なプロパティだけでなくプロトタイプチェーン上の列挙可能なプロパティも返されることに注意してください。)
Object.getOwnPropertyNames() ではプロトタイプチェーン上のプロパティは配列に含まれません:
function ParentClass() {}
ParentClass.prototype.inheritedMethod = function() {};
function ChildClass() {
this.prop = 5;
this.method = function() {};
}
ChildClass.prototype = new ParentClass;
ChildClass.prototype.prototypeMethod = function() {};
console.log(
Object.getOwnPropertyNames(
new ChildClass() // ["prop", "method"]
)
);
列挙不可能なプロパティのみを取得する
これには (Object.getOwnPropertyNames() で得られる) リストの全てのキーから (Object.keys() で得られる) 列挙可能なキーを取り除き、列挙不可能なキーのみを残すために Array.prototype.filter() 関数を使用します。
var target = myObject;
var enum_and_nonenum = Object.getOwnPropertyNames(target);
var enum_only = Object.keys(target);
var nonenum_only = enum_and_nonenum.filter(function(key) {
var indexInEnum = enum_only.indexOf(key);
if (indexInEnum == -1) {
// このキーが enum_only に無ければ、このキーは列挙不可能であることが分かります
// 列挙不可能なキーを残すために、この filter 関数の中で true を返します
return true;
} else {
return false;
}
});
console.log(nonenum_only);
注記
ES5 では、このメソッドの引数がオブジェクトではない値 (プリミティブ値) である場合、TypeError が発生します。ES6 では、非オブジェクト値はオブジェクトに強制変換されます。
Object.getOwnPropertyNames('foo');
// TypeError: "foo" is not an object (ES5 code)
Object.getOwnPropertyNames('foo');
// ["0", "1", "2", "length"] (ES6 code)
// ES6 の結果は ES5 における次のコードと等価です
Object.getOwnPropertyNames(Object('foo'));
仕様書
| Specification | Status | Comment |
|---|---|---|
| ECMAScript 5.1 (ECMA-262) Object.getOwnPropertyNames の定義 |
標準 | Initial definition. Implemented in JavaScript 1.8.5. |
| ECMAScript 2015 (6th Edition, ECMA-262) Object.getOwnPropertyNames の定義 |
標準 | |
| ECMAScript 2017 Draft (ECMA-262) Object.getOwnPropertyNames の定義 |
ドラフト |
ブラウザの互換性
Kangax's compat table に基づきます。
| 機能 | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| 基本サポート | 5 | 4.0 (2) | 9 | 12 | 5 |
| 機能 | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|
| 基本サポート | ? | ? | ? | ? | ? | ? |
SpiderMonkey-specific notes
SpiderMonkey 28 (Firefox 28 / Thunderbird 28 / SeaMonkey 2.25 / Firefox OS 1.3) より古いバージョンでは、Object.getOwnPropertyNames は Error オブジェクトの unresolved properties が取得できません。これはこのバージョン以降では修正されています。 (バグ 724768).
参考情報
- propertyIsEnumerable
- Object.create
- Object.keys
- Bug 518663 - ES5: Object.getOwnPropertyNames