Object.seal

suggest change

Object.seal prevents the addition or removal of properties from an object. Once an object has been sealed, a data property cannot be turned into an accessor, or the reverse. Unlike Object.freeze, it still allows existing properties to be edited.

Adding or deleting a property fails silently in non-strict code. In strict mode those attempts throw TypeError.

const obj = { foo: 'foo', bar: function () { return 'bar'; } };

Object.seal(obj)

obj.newFoo = 'newFoo';
obj.bar = function () { return 'foo' };

obj.newFoo; // undefined
obj.bar(); // 'foo'

// Can't make foo an accessor property
Object.defineProperty(obj, 'foo', { 
    get: function () { return 'newFoo'; }
}); // TypeError

// Changing writable from true to false is still allowed
Object.defineProperty(obj, 'foo', { 
    writable: false
});

obj.foo = 'newFoo';
obj.foo; // 'foo';

Adding a property in strict mode throws TypeError:

(function () {
    'use strict';

    const obj = { foo: 'foo' };

    Object.seal(obj);

    obj.newFoo = 'newFoo'; // TypeError
}());

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents