JavaScript Object.preventExtensions()
- Previous Page keys()
- Next Page prototype
- Go to Parent Page JavaScript Object Reference Manual
Definition and usage
Object.preventExtensions()
method to prevent adding new properties to an object.
You can use Object.isExtensible()
to check if an object is extensible.
Related methods:
Object.preventExtensions()
Modification is allowed, but adding properties is prevented.
Object.seal()
Modification is allowed, but adding and deleting properties is prevented.
Object.freeze()
Prevent modification, addition, and deletion of properties.
Object.isExtensible()
Returns true if the object is extensible.
Object.isSealed()
Returns true if the object is sealed.
Object.isFrozen()
Returns true if the object is frozen.
Instance
Example 1
// Create an object const person = {firstName: "Bill", lastName: "Gates"}; // Prevent extension Object.preventExtensions(person); // This will throw an error in strict mode person.nationality = "English";
Example 2
// Create an array const fruits = ["Banana", "Orange", "Apple", "Mango"]; Object.preventExtensions(fruits); // This will throw an error in strict mode: fruits.push("Kiwi");
Syntax
Object.preventExtensions(object)
Parameter
Parameter | Description |
---|---|
object | Required. The object to prevent extension. |
Return Value
Type | Description |
---|---|
Object | Modified object. |
Browser Support
Object.preventExtensions()
Is a feature of ECMAScript5 (ES5).
Since July 2013, all modern browsers have fully supported ES5 (JavaScript 2009):
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome 23 | IE/Edge 11 | Firefox 21 | Safari 6 | Opera 15 |
September 2012 | September 2012 | April 2013 | July 2012 | July 2013 |
- Previous Page keys()
- Next Page prototype
- Go to Parent Page JavaScript Object Reference Manual