Object seal() method

Find out all the information about the JavaScript seal() method of the Object object

The JavaScript seal() method of the Object object takes an object as a parameter and returns the same object. The object passed as a parameter is mutated, and now it is an object that does not accept new properties. New attributeCan'tAdd, and existing propertiesCan'tDeleted, but existing attributeswere ableWas changed.

example:

const dog = {}
dog.breed = 'Siberian Husky'
Object.seal(dog)
dog.breed = 'Pug'
dog.name = 'Roger' //TypeError: Cannot add property name, object is not extensible

Parameters passed as parameters will also be returned as parameters, sodog===myDog(This is the exact same object).

asObject.freeze()But it does not make the attribute unwritable. It can only prevent adding or deleting attributes.

asObject.preventExtensions()But it is also not allowed to delete attributes:

const dog = {}
dog.breed = 'Siberian Husky'
dog.name = 'Roger'
Object.seal(dog)
delete dog.name //TypeError: Cannot delete property 'name' of #<Object>

Download mine for freeJavaScript beginner's manual


More js tutorials: