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 der Object Instanzen gibt einen booleschen Wert zurück, der anzeigt, ob die angegebene Eigenschaft eine aufzählbare eigene 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

js
propertyIsEnumerable(prop)

Parameter

prop

Der Name der zu testenden Eigenschaft. Kann ein String oder ein Symbol sein.

Rückgabewert

Ein boolescher Wert, der anzeigt, ob die angegebene Eigenschaft aufzählbar und eine eigene Eigenschaft des Objekts ist.

Beschreibung

Alle Objekte, die von Object.prototype erben (das heißt, alle außer Objekte mit null-Prototyp), erben die Methode propertyIsEnumerable(). Diese Methode bestimmt, ob die angegebene Eigenschaft, String oder Symbol, eine aufzählbare eigene Eigenschaft des Objekts ist. Wenn das Objekt die angegebene Eigenschaft nicht besitzt, gibt diese Methode false zurück.

Diese Methode ist äquivalent zu Object.getOwnPropertyDescriptor(obj, prop)?.enumerable ?? false.

Beispiele

Verwendung von propertyIsEnumerable()

Das folgende Beispiel zeigt die Verwendung von propertyIsEnumerable() bei Objekten und Arrays.

js
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 aufzählbar, während benutzerdefinierte Objekteigenschaften oft aufzählbar sind, sofern nicht ausdrücklich anders festgelegt.

js
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 aufzählbare eigene Eigenschaften führen dazu, dass propertyIsEnumerable() true zurückgibt, obwohl alle aufzählbaren Eigenschaften, einschließlich der geerbten, durch die for...in-Schleife besucht werden.

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

Testen von Symboleigenschaften

Symbol Eigenschaften werden ebenfalls von propertyIsEnumerable() unterstützt. Beachten Sie, dass die meisten Enumerationsmethoden nur String-Eigenschaften besuchen; die Aufzählbarkeit von Symboleigenschaften ist nur dann nützlich, wenn Object.assign() oder Spread-Syntax verwendet wird. Weitere Informationen finden Sie unter 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

Verwendung mit Objekten ohne null-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.

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

Alternativ können Sie Object.getOwnPropertyDescriptor() verwenden, was ebenfalls hilft, zwischen nicht existierenden und tatsächlich nicht aufzählbaren Eigenschaften zu unterscheiden.

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

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

Spezifikationen

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

Browser-Kompatibilität

Siehe auch