Object.prototype.propertyIsEnumerable()

Object 实例的 propertyIsEnumerable() 方法返回一个布尔值,指示指定的属性是否是该对象的 可枚举自己的 属性。

¥The propertyIsEnumerable() method of Object instances returns a boolean indicating whether the specified property is this object's enumerable own property.

Try it

语法

¥Syntax

js
propertyIsEnumerable(prop)

参数

¥Parameters

prop

要测试的属性的名称。可以是字符串或 Symbol

返回值

¥Return value

一个布尔值,指示指定的属性是否可枚举并且是对象自己的属性。

¥A boolean value indicating whether the specified property is enumerable and is the object's own property.

描述

¥Description

所有从 Object.prototype 继承的对象(即除 null-原型对象 之外的所有对象)都继承 propertyIsEnumerable() 方法。此方法确定指定的属性(字符串或符号)是否是对象的可枚举自有属性。如果对象没有指定的属性,则此方法返回 false

¥All objects that inherit from Object.prototype (that is, all except null-prototype objects) inherit the propertyIsEnumerable() method. This method determines if the specified property, string or symbol, is an enumerable own property of the object. If the object does not have the specified property, this method returns false.

该方法相当于 Object.getOwnPropertyDescriptor(obj, prop)?.enumerable ?? false

¥This method is equivalent to Object.getOwnPropertyDescriptor(obj, prop)?.enumerable ?? false.

示例

¥Examples

使用 propertyIsEnumerable()

¥Using propertyIsEnumerable()

以下示例显示了 propertyIsEnumerable() 在对象和数组上的使用。

¥The following example shows the use of propertyIsEnumerable() on objects and arrays.

js
const o = {};
const a = [];
o.prop = "is enumerable";
a[0] = "is enumerable";

o.propertyIsEnumerable("prop"); // true
a.propertyIsEnumerable(0); // true

用户定义对象与内置对象

¥User-defined vs. built-in objects

默认情况下,大多数内置属性都是不可枚举的,而用户创建的对象属性通常是可枚举的,除非另有明确指定。

¥Most built-in properties are non-enumerable by default, while user-created object properties are often enumerable, unless explicitly designated otherwise.

js
const a = ["is enumerable"];

a.propertyIsEnumerable(0); // true
a.propertyIsEnumerable("length"); // false

Math.propertyIsEnumerable("random"); // false
globalThis.propertyIsEnumerable("Math"); // false

直接属性与继承属性

¥Direct vs. inherited properties

尽管所有可枚举属性(包括继承的属性)都会被 for...in 循环访问,但只有可枚举自己的属性才会导致 propertyIsEnumerable() 返回 true

¥Only enumerable own properties cause propertyIsEnumerable() to return true, although all enumerable properties, including inherited ones, are visited by the for...in loop.

js
const o1 = {
  enumerableInherited: "is enumerable",
};
Object.defineProperty(o1, "nonEnumerableInherited", {
  value: "is non-enumerable",
  enumerable: false,
});
const o2 = {
  // o1 is the prototype of o2
  __proto__: o1,
  enumerableOwn: "is enumerable",
};
Object.defineProperty(o2, "nonEnumerableOwn", {
  value: "is non-enumerable",
  enumerable: false,
});

o2.propertyIsEnumerable("enumerableInherited"); // false
o2.propertyIsEnumerable("nonEnumerableInherited"); // false
o2.propertyIsEnumerable("enumerableOwn"); // true
o2.propertyIsEnumerable("nonEnumerableOwn"); // false

测试符号属性

¥Testing symbol properties

propertyIsEnumerable() 也支持 Symbol 属性。请注意,大多数枚举方法仅访问字符串属性;符号属性的可枚举性仅在使用 Object.assign()扩展语法 时才有用。欲了解更多信息,请参阅 可枚举性和属性所有权

¥Symbol properties are also supported by propertyIsEnumerable(). Note that most enumeration methods only visit string properties; enumerability of symbol properties is only useful when using Object.assign() or spread syntax. For more information, see Enumerability and ownership of properties.

js
const sym = Symbol("enumerable");
const sym2 = Symbol("non-enumerable");
const o = {
  [sym]: "is enumerable",
};
Object.defineProperty(o, sym2, {
  value: "is non-enumerable",
  enumerable: false,
});

o.propertyIsEnumerable(sym); // true
o.propertyIsEnumerable(sym2); // false

与空原型对象一起使用

¥Usage with null-prototype objects

因为 null 原型对象不继承自 Object.prototype,所以它们不继承 propertyIsEnumerable() 方法。你必须使用对象作为 this 来调用 Object.prototype.propertyIsEnumerable

¥Because null-prototype objects do not inherit from Object.prototype, they do not inherit the propertyIsEnumerable() method. You must call Object.prototype.propertyIsEnumerable with the object as this instead.

js
const o = {
  __proto__: null,
  enumerableOwn: "is enumerable",
};

o.propertyIsEnumerable("enumerableOwn"); // TypeError: o.propertyIsEnumerable is not a function
Object.prototype.propertyIsEnumerable.call(o, "enumerableOwn"); // true

或者,你可以使用 Object.getOwnPropertyDescriptor() 来代替,这也有助于区分不存在的属性和实际上不可枚举的属性。

¥Alternatively, you may use Object.getOwnPropertyDescriptor() instead, which also helps to distinguish between non-existent properties and actually non-enumerable properties.

js
const o = {
  __proto__: null,
  enumerableOwn: "is enumerable",
};

Object.getOwnPropertyDescriptor(o, "enumerableOwn")?.enumerable; // true
Object.getOwnPropertyDescriptor(o, "nonExistent")?.enumerable; // undefined

规范

Specification
ECMAScript Language Specification
# sec-object.prototype.propertyisenumerable

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看