propertyIsEnumerable() method returns a Boolean indicating whether the specified property is enumerable and is the object's own property.

The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.

句法

obj.propertyIsEnumerable(prop)
					

参数

prop

The name of the property to test.

返回值

A 布尔 indicating whether the specified property is enumerable and is the object's own property.

描述

Every object has a propertyIsEnumerable method. This method can determine whether the specified property in an object can be enumerated by a for...in loop, with the exception of properties inherited through the prototype chain. If the object does not have the specified property, this method returns false .

范例

A basic use of propertyIsEnumerable

The following example shows the use of propertyIsEnumerable on objects and arrays:

var o = {};
var a = [];
o.prop = 'is enumerable';
a[0] = 'is enumerable';
o.propertyIsEnumerable('prop');   // returns true
a.propertyIsEnumerable(0);        // returns true
					

User-defined vs. built-in objects

The following example demonstrates the enumerability of user-defined vs. built-in properties:

var a = ['is enumerable'];
a.propertyIsEnumerable(0);          // returns true
a.propertyIsEnumerable('length');   // returns false
Math.propertyIsEnumerable('random');   // returns false
this.propertyIsEnumerable('Math');     // returns false
					

Direct vs. inherited properties

var a = [];
a.propertyIsEnumerable('constructor');         // returns false
function firstConstructor() {
  this.property = 'is not enumerable';
}
firstConstructor.prototype.firstMethod = function() {};
function secondConstructor() {
  this.method = function() { return 'is enumerable'; };
}
secondConstructor.prototype = new firstConstructor;
secondConstructor.prototype.constructor = secondConstructor;
var o = new secondConstructor();
o.arbitraryProperty = 'is enumerable';
o.propertyIsEnumerable('arbitraryProperty');   // returns true
o.propertyIsEnumerable('method');              // returns true
o.propertyIsEnumerable('property');            // returns false
o.property = 'is enumerable';
o.propertyIsEnumerable('property');            // returns true
// These return false as they are on the prototype which
// propertyIsEnumerable does not consider (even though the last two
// are iteratable with for-in)
o.propertyIsEnumerable('prototype');   // returns false (as of JS 1.8.1/FF3.6)
o.propertyIsEnumerable('constructor'); // returns false
o.propertyIsEnumerable('firstMethod'); // returns false
					

规范

规范
ECMAScript (ECMA-262)
The definition of 'Object.prototype.propertyIsEnumerable' in that specification.

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet Node.js
propertyIsEnumerable Chrome 1 Edge 12 Firefox 1 IE 5.5 Opera 4 Safari 3 WebView Android 1 Chrome Android 18 Firefox Android 4 Opera Android 10.1 Safari iOS 1 Samsung Internet Android 1.0 nodejs Yes

图例

完整支持

完整支持

另请参阅

元数据

  • 最后修改: