The preventExtensions() method of the object

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

The JavaScript preventExtensions() 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'tAdded, but existing attributeswere ableDelete and add existing attributeswere ableWas changed.

example:

const dog = {}
dog.breed = 'Siberian Husky'
Object.preventExtensions(dog)

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).

We cannot add new attributes, but we can delete existing attributes:

const dog = {}
dog.breed = 'Siberian Husky'
dog.name = 'Roger'
Object.preventExtensions(dog)
delete dog.name
dog //{ breed: 'Siberian Husky' }

Download mine for freeJavaScript beginner's manual


More js tutorials: