Object.preventExtensions() method prevents new properties from added to an object.
In the above code, after calling
preventExtensions on the
user object, when we try to add
preventExtensionsmethod returns the object being made non-extensible.
- There is no way to make an object
extensibleagain once it has been made
- To check if the object is
non-extensiblewe can use
preventExtensions()method only prevents addition of own properties. Properties can still be added to the object prototype.
preventExtensions() makes the
[[prototype]] of the
user object immutable, any
[[prototype]] re-assignment(adding new property) will throw a
TypeError. This behavior is specific to the internal
[[prototype]] property, other properties of the target object will remain mutable.
In other word when we try to change the
__proto__ prototype of the
- You can delete the property of the
You can also prevented from adding properties by using
A non-object argument to
preventExtensions will be treated as if it was a non-extensible ordinary object, return it.
Object.preventExtensions(1); // returns 1