Object.prototype.propertyIsEnumerable()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Die propertyIsEnumerable()
-Methode von Object
-Instanzen gibt einen boolean zurück, der anzeigt, ob die angegebene Eigenschaft eine auflistbare Eigen- Eigenschaft dieses Objekts ist.
Probieren Sie es aus
const object1 = {};
const array1 = [];
object1.property1 = 42;
array1[0] = 42;
console.log(object1.propertyIsEnumerable("property1"));
// Expected output: true
console.log(array1.propertyIsEnumerable(0));
// Expected output: true
console.log(array1.propertyIsEnumerable("length"));
// Expected output: false
Syntax
propertyIsEnumerable(prop)
Parameter
Rückgabewert
Ein boolean-Wert, der anzeigt, ob die angegebene Eigenschaft aufzählbar und eine eigene Eigenschaft des Objekts ist.
Beschreibung
Alle Objekte, die von Object.prototype
erben (d.h. alle außer Objekte mit null
-Prototyp), erben die Methode propertyIsEnumerable()
. Diese Methode bestimmt, ob die angegebene Eigenschaft, sei es ein String oder Symbol, eine auflistbare Eigen-Eigenschaft des Objekts ist. Falls das Objekt die angegebene Eigenschaft nicht besitzt, gibt diese Methode false
zurück.
Diese Methode ist gleichbedeutend mit Object.getOwnPropertyDescriptor(obj, prop)?.enumerable ?? false
.
Beispiele
Verwendung von propertyIsEnumerable()
Das folgende Beispiel zeigt die Verwendung von propertyIsEnumerable()
bei Objekten und Arrays.
const o = {};
const a = [];
o.prop = "is enumerable";
a[0] = "is enumerable";
o.propertyIsEnumerable("prop"); // true
a.propertyIsEnumerable(0); // true
Benutzerdefinierte vs. eingebaute Objekte
Die meisten eingebauten Eigenschaften sind standardmäßig nicht auflistbar, während selbst erstellte Objekteigenschaften oft auflistbar sind, sofern nicht ausdrücklich anders angegeben.
const a = ["is enumerable"];
a.propertyIsEnumerable(0); // true
a.propertyIsEnumerable("length"); // false
Math.propertyIsEnumerable("random"); // false
globalThis.propertyIsEnumerable("Math"); // false
Direkte vs. geerbte Eigenschaften
Nur auflistbare Eigen-Eigenschaften lassen propertyIsEnumerable()
true
zurückgeben, obwohl alle auflistbaren Eigenschaften, einschließlich geerbter, durch die for...in
-Schleife besucht werden.
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
Testen von Symbol-Eigenschaften
Auch Symbol
-Eigenschaften werden von propertyIsEnumerable()
unterstützt. Beachten Sie jedoch, dass die meisten Enumerationsmethoden nur String-Eigenschaften besuchen; die Aufzählbarkeit von Symbol-Eigenschaften ist nur bei der Verwendung von Object.assign()
oder der Spread-Syntax nützlich. Weitere Informationen finden Sie unter Aufzählbarkeit und Besitz von Eigenschaften.
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
Verwendung mit Objekten ohne Prototyp
Da Objekte mit null
-Prototyp nicht von Object.prototype
erben, erben sie auch nicht die Methode propertyIsEnumerable()
. Sie müssen Object.prototype.propertyIsEnumerable
mit dem Objekt als this
aufrufen.
const o = {
__proto__: null,
enumerableOwn: "is enumerable",
};
o.propertyIsEnumerable("enumerableOwn"); // TypeError: o.propertyIsEnumerable is not a function
Object.prototype.propertyIsEnumerable.call(o, "enumerableOwn"); // true
Alternativ können Sie Object.getOwnPropertyDescriptor()
verwenden, was auch dabei hilft, zwischen nicht vorhandenen und tatsächlich nicht auflistbaren Eigenschaften zu unterscheiden.
const o = {
__proto__: null,
enumerableOwn: "is enumerable",
};
Object.getOwnPropertyDescriptor(o, "enumerableOwn")?.enumerable; // true
Object.getOwnPropertyDescriptor(o, "nonExistent")?.enumerable; // undefined
Spezifikationen
Specification |
---|
ECMAScript® 2025 Language Specification # sec-object.prototype.propertyisenumerable |
Browser-Kompatibilität
Report problems with this compatibility data on GitHubdesktop | mobile | server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
propertyIsEnumerable |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support