propertyIsEnumerable()
方法返回一个布尔值,表示指定的属性是否可枚举。
obj.propertyIsEnumerable(prop)
prop
Boolean
。每个对象都有一个propertyIsEnumerable
方法。此方法可以确定对象中指定的属性是否可以被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
// 这些返回fasle,是因为,在原型链上propertyIsEnumerable不被考虑
// (尽管最后两个在for-in循环中可以被循环出来)。
o.propertyIsEnumerable('prototype'); // 返回 false (根据 JS 1.8.1/FF3.6)
o.propertyIsEnumerable('constructor'); // 返回 false
o.propertyIsEnumerable('firstMethod'); // 返回 false
Specification | Status | Comment |
---|---|---|
ECMAScript 3rd Edition (ECMA-262) | Standard | Initial definition |
ECMAScript 5.1 (ECMA-262) Object.prototype.propertyIsEnumerable |
Standard | |
ECMAScript 2015 (6th Edition, ECMA-262) Object.prototype.propertyIsEnumerable |
Standard | |
ECMAScript Latest Draft (ECMA-262) Object.prototype.propertyIsEnumerable |
Draft |
Desktop | Mobile | Server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
propertyIsEnumerable | Chrome Full support Yes | Edge Full support 12 | Firefox Full support 1 | IE Full support Yes | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes | nodejs Full support Yes |