Object.isSealed()

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 statische Methode Object.isSealed() überprüft, ob ein Objekt versiegelt ist.

Probieren Sie es aus

const object1 = {
  property1: 42,
};

console.log(Object.isSealed(object1));
// Expected output: false

Object.seal(object1);

console.log(Object.isSealed(object1));
// Expected output: true

Syntax

js
Object.isSealed(obj)

Parameter

obj

Das Objekt, das überprüft werden soll.

Rückgabewert

Ein Boolean, der angibt, ob das angegebene Objekt versiegelt ist oder nicht.

Beschreibung

Gibt true zurück, wenn das Objekt versiegelt ist, andernfalls false. Ein Objekt ist versiegelt, wenn es nicht erweiterbar ist und alle seine Eigenschaften nicht konfigurierbar sind und daher nicht entfernt werden können (aber nicht unbedingt schreibgeschützt).

Beispiele

Verwendung von Object.isSealed

js
// Objects aren't sealed by default.
const empty = {};
Object.isSealed(empty); // false

// If you make an empty object non-extensible,
// it is vacuously sealed.
Object.preventExtensions(empty);
Object.isSealed(empty); // true

// The same is not true of a non-empty object,
// unless its properties are all non-configurable.
const hasProp = { fee: "fie foe fum" };
Object.preventExtensions(hasProp);
Object.isSealed(hasProp); // false

// But make them all non-configurable
// and the object becomes sealed.
Object.defineProperty(hasProp, "fee", {
  configurable: false,
});
Object.isSealed(hasProp); // true

// The easiest way to seal an object, of course,
// is Object.seal.
const sealed = {};
Object.seal(sealed);
Object.isSealed(sealed); // true

// A sealed object is, by definition, non-extensible.
Object.isExtensible(sealed); // false

// A sealed object might be frozen,
// but it doesn't have to be.
Object.isFrozen(sealed); // true
// (all properties also non-writable)

const s2 = Object.seal({ p: 3 });
Object.isFrozen(s2); // false
// ('p' is still writable)

const s3 = Object.seal({
  get p() {
    return 0;
  },
});
Object.isFrozen(s3); // true
// (only configurability matters for accessor properties)

Nicht-Objekt-Argument

In ES5 verursacht diese Methode einen TypeError, wenn das Argument kein Objekt (ein primitives Datenelement) ist. In ES2015 wird true zurückgegeben, ohne dass ein Fehler auftritt, wenn ein Nicht-Objekt-Argument übergeben wird, da primitive Datenelemente per Definition unveränderlich sind.

js
Object.isSealed(1);
// TypeError: 1 is not an object (ES5 code)

Object.isSealed(1);
// true                          (ES2015 code)

Spezifikationen

Specification
ECMAScript® 2025 Language Specification
# sec-object.issealed

Browser-Kompatibilität

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
isSealed

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

Siehe auch