Object.seal
suggest changeObject.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
}());
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents