The Object.preventExtensions()
method in JavaScript prevents new properties from being added to an object.
The preventExtensions method in JavaScript is used to prevent any further additions of properties to an object. When invoked on an object, it marks the object as non-extensible, meaning no new properties or methods can be added to it.
What Is the preventExtensions Method in JavaScript?
The preventExtensions
method prevents new properties from being added to an object by marking the object as non-extensible. It’s used when you want to ensure an object’s structure remains consistent throughout its lifecycle.
This method ensures that the shape of the object remains fixed and prevents accidental or unintended modifications to its structure. Existing properties can still be modified or deleted, but new ones cannot be added. The preventExtensions method is often used when you want to enforce immutability or ensure that an object’s structure remains consistent throughout its lifecycle.
How Does PreventExtensions in JavaScript Work?
"use strict";
let user = { name : "Jagathish" };
Object.preventExtensions(user);
try {
user.age = 23;
} catch (e) {
console.log(e);
// expected output: TypeError:Cannot add property age, object is not extensible
}
In the above code, we called preventExtensions
on the user
object. When we try to add an age
property, however, JavaScript will throw a TypeError
.
preventExtensions
method returns the object being made non-extensible.- There is no way to make an object
extensible
again once it has been madenon-extensible
. - To check if the object is
non-extensible
, we can useObject.isExtensible()
.
"use strict";
let user = { name : "Jagathish" };
Object.isExtensible(user); // true
Object.preventExtensions(user);
Object.isExtensible(user); // false
preventExtensions()
method only prevents addition of own properties. Properties can still be added to the object prototype.
"use strict";
let user = { name : "Jagathish" };
Object.preventExtensions(user);
// user.age = 23; --> this will throw error
user.__proto__.age = 23; // adding property to object prototype
console.log(user); // {name : "Jagathish"}
console.log(user.age); // 23 --> this value comes from object prototype property
How to Use PreventExpressions in JavaScript
preventExtensions()
makes the [[prototype]]
of the user object immutable, any [[prototype]]
re-assignment (adding a 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 words, when we try to change the __proto__
prototype of the non-extensible
object , JavaScript will throw an error.
"use strict";
let empty = Object.preventExtensions({});
empty.__proto__ = { name: 'jagathish' }; // TypeError : #<Object> is not extensible
You can delete the property of the non-extensible
object.
"use strict";
let user = { name : "Jagathish", age : 23 };
Object.preventExtensions(user);
delete user.age;
console.log(user); //{name: "Jagathish"}
You can also prevent it from adding properties by using defineProperty
.
"use strict";
let user = { name : "Jagathish" };
Object.preventExtensions(user);
try {
Object.defineProperty(user, 'age', {
value: 23
});
} catch (e) {
console.log(e);
// expected output: TypeError:Cannot define property age, object is not extensible
}
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
PreventExtensions Method Example
One real-world use case for the preventExtensions
method in JavaScript is when you want to protect a configuration object from having additional properties added to it. Let’s say you have a settings object that defines various configuration options for your application, and you want to ensure that these options are not accidentally modified or extended.
const settings = {
theme: 'dark',
fontSize: '16px',
language: 'en'
};
Object.preventExtensions(settings);
settings.theme = 'light'; // Modifying existing property is allowed
settings.newOption = 'value'; // Error: Cannot add property 'newOption', object is not extensible
console.log(settings); // { theme: 'light', fontSize: '16px', language: 'en' }
In the code example above, the preventExtensions method is used to make the settings object non-extensible. This ensures that no new properties can be added to the object after invoking the method.
Consequently, attempting to add a new property (newOption
) results in an error. However, modifying existing properties (theme
) is still allowed.