Object.prototype.propertyIsEnumerable()
propertyIsEnumerable() 方法属于 Object 实例,用于返回一个布尔值,表明指定的属性是该对象的 可枚举的自有 属性。
试一试
const object = {};
const array = [];
object.foo = 42;
array[0] = 42;
console.log(object.propertyIsEnumerable("foo"));
// Expected output: true
console.log(array.propertyIsEnumerable(0));
// Expected output: true
console.log(array.propertyIsEnumerable("length"));
// Expected output: false
语法
propertyIsEnumerable(prop)
参数
返回值
一个布尔值,指示指定的属性是否是可枚举的,并且是该对象自身的属性。
描述
所有继承自 Object.prototype 的对象(即除了 null-prototype 对象 之外的所有对象)都继承了 propertyIsEnumerable() 方法。此方法用于确定指定的属性(字符串或符号)是否是对象的、可枚举的自有属性。如果对象不具有指定的属性,此方法将返回 false。
此方法等同于 Object.getOwnPropertyDescriptor(obj, prop)?.enumerable ?? false。
示例
使用 propertyIsEnumerable()
以下示例展示了 propertyIsEnumerable() 在对象和数组上的用法。
const o = {};
const a = [];
o.prop = "is enumerable";
a[0] = "is enumerable";
o.propertyIsEnumerable("prop"); // true
a.propertyIsEnumerable(0); // true
用户定义对象与内置对象
大多数内置属性默认是不可枚举的,而用户创建的对象属性通常是可枚举的,除非明确指定。
const a = ["is enumerable"];
a.propertyIsEnumerable(0); // true
a.propertyIsEnumerable("length"); // false
Math.propertyIsEnumerable("random"); // false
globalThis.propertyIsEnumerable("Math"); // false
直接属性与继承属性
只有可枚举的自有属性才会导致 propertyIsEnumerable() 返回 true,尽管包括继承在内的所有可枚举属性都会被 for...in 循环遍历。
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
测试符号属性
propertyIsEnumerable() 也支持 Symbol 属性。请注意,大多数枚举方法只访问字符串属性;符号属性的可枚举性仅在使用 Object.assign() 或 展开语法 时才有用。有关更多信息,请参阅 属性的可枚举性和所有权。
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
与 null-prototype 对象一起使用
由于 null-prototype 对象不继承自 Object.prototype,因此它们不继承 propertyIsEnumerable() 方法。您必须将对象作为 this 来调用 Object.prototype.propertyIsEnumerable。
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(),它也有助于区分不存在的属性和实际不可枚举的属性。
const o = {
__proto__: null,
enumerableOwn: "is enumerable",
};
Object.getOwnPropertyDescriptor(o, "enumerableOwn")?.enumerable; // true
Object.getOwnPropertyDescriptor(o, "nonExistent")?.enumerable; // undefined
规范
| 规范 |
|---|
| ECMAScript® 2026 语言规范 # sec-object.prototype.propertyisenumerable |
浏览器兼容性
加载中…