The Reflect.preventExtensions method is employed to prohibit any further extensions to an object. It serves the same purpose as the Object.preventExtensions method.
Syntax:
Example
Reflect.preventExtensions(obj)
Parameters:
Subject: This is the target entity for which extensions are to be restricted.
Return value:
The function will return a boolean value of true if the target has been effectively set to block extensions. If not successful, the function will return false.
Exceptions:
A TypeError, if the target is not an Object.
Browser Support:
| Chrome | 49 |
|---|---|
Edge |
12 |
| Firefox | 42 |
| Opera | 36 |
Example 1
Example
const u = {};
console.log ( Reflect.isExtensible ( u ) );
Reflect.preventExtensions ( u );
console.log ( Reflect.isExtensible ( u ) );
Output:
Output
true
false
Example 2
Example
const obj = {"p":3,"q":4};
Reflect.preventExtensions(obj);
console.log ( obj.hasOwnProperty ( "p" ) );
delete obj.p;
console.log ( obj.hasOwnProperty ( "q" ) );
Output:
Output
true
true
Example 3
Example
const uu = {};
Reflect.preventExtensions(uu);
console.log(
Reflect.isExtensible(uu)
);
// add a property
uu.pp = 3;
console.log(
uu.hasOwnProperty ("pp")
);
Output:
Output
false
false