mozilla
Your Search Results

    seal

    Summary

    The Object.seal() method 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.

    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__ ( ) 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" }); // changes existing property value

    Specifications

    Specification Status Comment
    ECMAScript Language Specification 5.1th Edition (ECMA-262) Standard Initial definition.
    Implemented in JavaScript 1.8.5
    ECMAScript Language Specification 6th Edition (ECMA-262) Draft  

    Browser compatibility

    Feature Firefox (Gecko) Chrome Internet Explorer Opera Safari
    Basic support 4 (2.0) 6 9 Not supported 5.1
    Feature Firefox Mobile (Gecko) Android IE Mobile Opera Mobile Safari Mobile
    Basic support ? ? ? ? ?

    Based on Kangax's compat table.

    See also

    Document Tags and Contributors

    Contributors to this page: ethertank
    Last updated by: ethertank,