Using Object.preventExtensions ensures that no new properties can be added to an object, effectively blocking any future extensions to the object. This action is irreversible, as once an object is rendered non-extensible, it cannot be reverted back to an extensible state.
Syntax:
Object.preventExtensions(obj)
Parameter:
obj: This is the object that needs to be made non-extensible.
Return value:
It returns the object being made non-extensible.
Browser Support:
| Chrome | 6 |
|---|---|
Edge |
Yes |
| Firefox | 4 |
| Opera | 12 |
Example 1
const uu = {};
Object.preventExtensions(uu);
console.log(
Object.isExtensible(uu)
);
Output:
Example 2
const obj = {};
Object.preventExtensions(obj);
obj.o = 3;
console.log(
obj.hasOwnProperty("o")
);
Output:
Example 3
const t = {"p":3};
Object.preventExtensions(t);
delete t.p;
console.log ( t.hasOwnProperty ( "p" ) );
//expected output: false
Output: