mozilla

Revision 15994 of Object.seal()

  • Revision slug: JavaScript/Reference/Global_Objects/Object/seal
  • Revision title: seal
  • Revision id: 15994
  • Created:
  • Creator: XP1
  • Is current revision? No
  • Comment 1 words added

Revision Content

{{ js_minversion_header("1.8.5") }}

Summary

Seals an object, preventing new properties from being added to it and marking all existing properties as non-configurable. Values of present properties can still be changed as long as they are writable.

Method of Object
Implemented in JavaScript 1.8.5
ECMAScript Edition ECMAScript 5th Edition

Syntax

Object.seal(obj)

Parameters

obj
The object which should be sealed.

Description

By default, objects are extensible (new properties can be added to them). Sealing an object prevents new properties from being added and marks all existing properties as non-configurable. This has the effect of making the set of properties on the object fixed and immutable. Making all properties non-configurable also prevents them from being converted from data properties to accessor properties and vice versa, but it does not prevent the values of data properties from being changed. Attempting to delete or add properties to a sealed object, or to convert a data property to accessor or vice versa, will fail, either silently or by throwing a TypeError (most commonly, although not exclusively, when in strict mode code).

The prototype chain remains untouched. However, the __proto__ ( {{ deprecated_inline() }} ) property is sealed as well.

Examples

var obj = {
    prop: function () {},
    foo: "bar"
  };

// New properties may be added, existing properties may be changed or removed
obj.foo = "baz";
obj.lumpy = "woof";
delete obj.prop;

var o = Object.seal(obj);
assert(o === obj);
assert(Object.isSealed(obj) === true);

// Changing property values on a sealed object still works.
obj.foo = "quux";

// But you can't convert data properties to accessors, or vice versa.
Object.defineProperty(obj, "foo", { get: function() { return "g"; } }); // throws a TypeError

// Now any changes, other than to property values, will fail.
obj.quaxxor = "the friendly duck"; // silently doesn't add the property
delete obj.foo; // silently doesn't delete the property

// ...and in strict mode such attempts will throw TypeErrors
function fail() {
  "use strict";
  delete obj.foo; // throws a TypeError
  obj.sparky = "arf"; // throws a TypeError
}
fail();

// Attempted additions through Object.defineProperty will also throw
Object.defineProperty(obj, "ohai", { value: 17 }); // throws a TypeError
Object.defineProperty(obj, "foo", { value: "eit" }); // throws a TypeError

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

<p>{{ js_minversion_header("1.8.5") }}</p>
<h2 name="Summary">Summary</h2>
<p>Seals an object, preventing new properties from being added to it and marking all existing properties as non-configurable. Values of present properties can still be changed as long as they are writable.</p>
<table class="standard-table"> <thead> <tr> <th class="header" colspan="2">Method of <a href="/en/JavaScript/Reference/Global_Objects/Object" title="en/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 name="Syntax">Syntax</h2>
<p><code>Object.seal(<em>obj</em>)</code></p>
<h2 name="Parameters">Parameters</h2>
<dl> <dt>obj</dt> <dd>The object which should be sealed.</dd>
</dl>
<h2 name="Description">Description</h2>
<p>By default, objects are <a href="/en/JavaScript/Reference/Global_Objects/Object/isExtensible" title="en/JavaScript/Reference/Global Objects/Object/isExtensible">extensible</a> (new properties can be added to them). Sealing an object prevents new properties from being added and marks all existing properties as non-configurable. This has the effect of making the set of properties on the object fixed and immutable. Making all properties non-configurable also prevents them from being converted from data properties to accessor properties and vice versa, but it does not prevent the values of data properties from being changed. Attempting to delete or add properties to a sealed object, or to convert a data property to accessor or vice versa, will fail, either silently or by throwing a <code>TypeError</code> (most commonly, although not exclusively, when in <a href="/en/JavaScript/Reference/Functions_and_function_scope/Strict_mode" title="en/JavaScript/Strict mode">strict mode</a> code).</p>
<p>The prototype chain remains untouched. However, the <a href="/en/JavaScript/Reference/Global_Objects/Object/proto" title="en/JavaScript/Reference/Global Objects/Object/proto">__proto__</a> ( {{ deprecated_inline() }} ) property is sealed as well.</p>
<h3 name="Examples">Examples</h3>
<pre class="brush: js">var obj = {
    prop: function () {},
    foo: "bar"
  };

// New properties may be added, existing properties may be changed or removed
obj.foo = "baz";
obj.lumpy = "woof";
delete obj.prop;

var o = Object.seal(obj);
assert(o === obj);
assert(Object.isSealed(obj) === true);

// Changing property values on a sealed object still works.
obj.foo = "quux";

// But you can't convert data properties to accessors, or vice versa.
Object.defineProperty(obj, "foo", { get: function() { return "g"; } }); // throws a TypeError

// Now any changes, other than to property values, will fail.
obj.quaxxor = "the friendly duck"; // silently doesn't add the property
delete obj.foo; // silently doesn't delete the property

// ...and in strict mode such attempts will throw TypeErrors
function fail() {
  "use strict";
  delete obj.foo; // throws a TypeError
  obj.sparky = "arf"; // throws a TypeError
}
fail();

// Attempted additions through Object.defineProperty will also throw
Object.defineProperty(obj, "ohai", { value: 17 }); // throws a TypeError
Object.defineProperty(obj, "foo", { value: "eit" }); // throws a TypeError</pre>
<h2>Cross-browser compatibility</h2>
<p>{{ CompatibilityTable() }}</p>
<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 class=" external" href="http://kangax.github.com/es5-compat-table/">Kangax's compat table</a>.</p><h2 name="See_also">See also</h2>
<ul> <li><strong><a href="/en/JavaScript/Reference/Global_Objects/Object/isSealed" title="en/Core
    JavaScript 1.5 Reference/Global Objects/Object/isSealed"><code>Object.isSealed</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><strong><a href="/en/JavaScript/Reference/Global_Objects/Object/isSealed" title="en/Core
    JavaScript 1.5 Reference/Global Objects/Object/isSealed"><code><br> </code></a></strong></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