freeze() makes an object immune to everything even little changes cannot be made. Object. seal() prevents from deletion of existing properties but cannot prevent them from external changes.
freeze() The Object. freeze() method freezes an object. Freezing an object prevents extensions and makes existing properties non-writable and non-configurable.
Object. freeze works on values, and more specifically, object values. It makes an object immutable, i.e. you cannot change its properties. Basically, const is the new var ; it's just block-scoped and prevents reassignment.
freeze() which is used to freeze an object. Freezing an object does not allow new properties to be added to an object and prevents from removing or altering the existing properties. Object. freeze() preserves the enumerability, configurability, writability and the prototype of the object.
Object.seal
delete
will return falsewritable
attribute, and their value
attribute if writeable
is true).TypeError
when attempting to modify the value of the sealed object itself (most commonly in strict mode)Object.freeze
Object.seal
does, plus:Neither one affects 'deep'/grandchildren objects. E.g., if obj
is frozen, obj.el
can’t be reassigned, but the value of obj.el
could be modified, e.g. obj.el.id
can be changed.
Sealing or freezing an object may affect its enumeration speed, depending on the browser:
Tests: Sealed objects, Frozen objects.
I wrote a test project which compares these 3 methods:
Object.freeze()
Object.seal()
Object.preventExtensions()
My unit tests cover CRUD cases:
Result:
You can always looks these up in MDN. In short:
Object.freeze()
creates a frozen object, which means it takes an existing object and essentially callsObject.seal()
on it, but it also marks all “data accessor” properties aswritable:false
, so that their values cannot be changed.
-- Kyle Simpson, You Don't Know JS - This & Object Prototypes
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With