Object.isSealed()

  • Revision slug: Web/JavaScript/Reference/Global_Objects/Object/isSealed
  • Revision title: Object.isSealed
  • Revision id: 417903
  • Created:
  • Creator: Sheppy
  • Is current revision? No
  • Comment title.Moved From JavaScript/Reference/Global_Objects/Object/isSealed to Web/JavaScript/Reference/Global_Objects/Object/isSealed

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 ECMAScript 5th Edition

Syntax

Object.isSealed(obj)

Parameters

obj
The object which should be checked.

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

Cross-browser compatibility

{{CompatibilityTable}}
Feature Firefox (Gecko) Chrome Internet Explorer Opera Safari
Basic support 4 (2.0) 6 9 {{CompatNo}} 5.1
Feature Firefox Mobile (Gecko) Android IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}

Based on Kangax's compat table.

See also

Revision Source

<div>
  {{js_minversion_header("1.8.5")}}</div>
<h2 id="Summary">Summary</h2>
<p>Determine if an object is sealed.</p>
<table class="standard-table">
  <thead>
    <tr>
      <th class="header" colspan="2">Method of <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object" title="JavaScript/Reference/Global_Objects/Object"><code>Object</code></a></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Implemented in</td>
      <td>JavaScript 1.8.5</td>
    </tr>
    <tr>
      <td>ECMAScript Edition</td>
      <td>ECMAScript 5th Edition</td>
    </tr>
  </tbody>
</table>
<h2 id="Syntax">Syntax</h2>
<pre class="syntaxbox">
<code>Object.isSealed(<em>obj</em>)</code></pre>
<h2 id="Parameters">Parameters</h2>
<dl>
  <dt>
    obj</dt>
  <dd>
    The object which should be checked.</dd>
</dl>
<h2 id="Description">Description</h2>
<p>Returns true if the object is sealed, otherwise false. An object is sealed if it is <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object/isExtensible" title="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 id="Examples" 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>
<h2 id="Cross-browser_compatibility">Cross-browser compatibility</h2>
<div>
  {{CompatibilityTable}}</div>
<div id="compat-desktop">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Firefox (Gecko)</th>
        <th>Chrome</th>
        <th>Internet Explorer</th>
        <th>Opera</th>
        <th>Safari</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>4 (2.0)</td>
        <td>6</td>
        <td>9</td>
        <td>{{CompatNo}}</td>
        <td>5.1</td>
      </tr>
    </tbody>
  </table>
</div>
<div id="compat-mobile">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Firefox Mobile (Gecko)</th>
        <th>Android</th>
        <th>IE Mobile</th>
        <th>Opera Mobile</th>
        <th>Safari Mobile</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>{{CompatUnknown}}</td>
        <td>{{CompatUnknown}}</td>
        <td>{{CompatUnknown}}</td>
        <td>{{CompatUnknown}}</td>
        <td>{{CompatUnknown}}</td>
      </tr>
    </tbody>
  </table>
</div>
<p>Based on <a href="http://kangax.github.com/es5-compat-table/">Kangax's compat table</a>.</p>
<h2 id="See_also">See also</h2>
<ul>
  <li><strong><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object/seal" title="JavaScript/Reference/Global Objects/Object/seal"><code>Object.seal</code></a></strong></li>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object/preventExtensions" title="JavaScript/Reference/Global Objects/Object/preventExtensions"><code>Object.preventExtensions</code></a></li>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object/isExtensible" title="JavaScript/Reference/Global Objects/Object/isExtensible"><code>Object.isExtensible</code></a></li>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object/freeze" title="JavaScript/Reference/Global Objects/Object/freezed"><code>Object.freeze</code></a></li>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object/isFrozen" title="JavaScript/Reference/Global Objects/Object/isFrozen"><code>Object.isFrozen</code></a></li>
</ul>
Revert to this revision