Apply your JS skills to key Mozilla projects as an MDN Fellow! http://mzl.la/MDNFellowship

mozilla

Revision 16009 of Object.isSealed()

  • Revision slug: JavaScript/Reference/Global_Objects/Object/isSealed
  • Revision title: isSealed
  • Revision id: 16009
  • Created:
  • Creator: evilpie
  • Is current revision? No
  • Comment Reverted to earlier version; 7 words added, 1 words removed

Revision Content

{{ js_minversion_header("1.8.5") }}

Summary

Determine if an object is sealed.

Method of Object
Implemented in: JavaScript 1.8.5
ECMAScript Edition: ECMA-262 5th Edition

Syntax

Object.isSealed(obj)

Parameters

obj
The object which should be checked.
sealed
boolean which indicates if the object is sealed.

Description

Returns true if the object is sealed, otherwise false. An object is sealed if it is non-extensible and if all its properties are non-configurable and therefore not removable (but not necessarily non-writable).

Examples

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

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

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

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

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

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

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

var s2 = Object.seal({ p: 3 });
assert(Object.isFrozen(s2) === false); // "p" is still writable

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

See also

Revision Source

<p>{{ js_minversion_header("1.8.5") }}</p>
<h3 name="Summary">Summary</h3>
<p>Determine if an object is sealed.</p>
<table class="fullwidth-table"> <tbody> <tr> <td class="header" colspan="2">Method of <a href="/en/JavaScript/Reference/Global_Objects/Object" title="en/JavaScript/Reference/Global_Objects/Object">Object</a></td> </tr> <tr> <td>Implemented in:</td> <td>JavaScript 1.8.5</td> </tr> <tr> <td>ECMAScript Edition:</td> <td>ECMA-262 5th Edition</td> </tr> </tbody>
</table>
<h3 name="Syntax">Syntax</h3>
<p><code>Object.isSealed(<em>obj</em>)</code></p>
<h3 name="Parameters">Parameters</h3>
<dl> <dt>obj</dt> <dd>The object which should be checked.</dd> <dt><code>sealed<br> </code></dt> <dd>boolean which indicates if the object is sealed.</dd>
</dl>
<h3 name="Description">Description</h3>
<p>Returns true if the object is sealed, otherwise false. An object is sealed if it is <a href="/en/JavaScript/Reference/Global_Objects/Object/isExtensible" title="en/JavaScript/Reference/Global Objects/Object/isExtensible">non-extensible</a> and if all its properties are non-configurable and therefore not removable (but not necessarily non-writable).</p>
<h3 name="Examples">Examples</h3>
<pre class="brush: js">// Objects aren't sealed by default.
var empty = {};
assert(Object.isSealed(empty) === false);

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

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

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

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

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

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

var s2 = Object.seal({ p: 3 });
assert(Object.isFrozen(s2) === false); // "p" is still writable

var s3 = Object.seal({ get p() { return 0; } });
assert(Object.isFrozen(s3) === true); // only configurability matters for accessor properties
</pre>
<h3 name="See_also">See also</h3>
<ul> <li><strong><a href="/en/JavaScript/Reference/Global_Objects/Object/seal" title="en/Core
    JavaScript 1.5 Reference/Global Objects/Object/seal"><code>Object.seal</code></a></strong></li> <li><a href="/en/JavaScript/Reference/Global_Objects/Object/preventExtensions" title="en/Core
    JavaScript 1.5 Reference/Global Objects/Object/preventExtensions"><code>Object.preventExtensions</code></a></li> <li><a href="/en/JavaScript/Reference/Global_Objects/Object/isExtensible" title="en/Core
    JavaScript 1.5 Reference/Global Objects/Object/isExtensible"><code>Object.isExtensible</code></a><a href="/en/JavaScript/Reference/Global_Objects/Object/seal" title="en/Core
    JavaScript 1.5 Reference/Global Objects/Object/seal"><code><br> </code></a></li> <li><a href="/en/JavaScript/Reference/Global_Objects/Object/freeze" title="en/Core
    JavaScript 1.5 Reference/Global Objects/Object/freezed"><code>Object.freeze</code></a></li> <li><a href="/en/JavaScript/Reference/Global_Objects/Object/isFrozen" title="en/Core
    JavaScript 1.5 Reference/Global Objects/Object/isFrozen"><code>Object.isFrozen</code></a></li>
</ul>
Revert to this revision