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 |
瀏覽器相容性
載入中…