JavaScript TypeError – Can’t define property “X”: “Obj” is not extensible
This JavaScript exception can’t define property “x”: “obj” is not extensible occurs when Object.preventExtensions() is used on an object to make it no longer extensible, So now, New properties can not be added to the object.
Message:
TypeError: Cannot create property for a non-extensible object (Edge)
TypeError: can't define property "x": "obj" is not extensible (Firefox)
TypeError: Cannot define property: "x", object is not extensible. (Chrome)
Error Type:
TypeError
Cause of Error: After the Object.preventExtensions() method is applied to an object, New properties are added to the object, Which is not permissible.
Example 1: In this example, the new property is being added after the Object.preventExtensions() method is applied, So the error has occurred.
Javascript
'use strict' ;
let GFG_Obj = { 'name' : 'GFG' };
Object.preventExtensions(GFG_Obj);
GFG_Obj.age = 22;
|
Output(in console):
TypeError: Cannot create property for a non-extensible object
Example 2: In this example, the new property is being added using defineProperty() method after the Object.preventExtensions() method is applied, So the error has occurred.
Javascript
'use strict' ;
let GFG_Obj = { 'name' : 'GFG' };
Object.preventExtensions(GFG_Obj);
Object.defineProperty(GFG_Obj,
'person' , { dob: "02/11/1997" }
);
|
Output(in console):
TypeError: Cannot define property 'person': object is not extensible
Last Updated :
02 Jun, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...