Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

JavaScript | Reflect.preventExtensions() Method

  • Last Updated : 30 Sep, 2021

The Reflect.preventExtensions() method in Javascript is used to prevent the future extensions to the object which means prevent from adding the new properties to the object.
Syntax: 
 

Reflect.preventExtensions( obj )  

Parameters: This method accepts a single parameter as mentioned above and described below: 
 

  • Obj: This parameter holds the target object and it is used to prevent extensions.

Return value: This method returns the Boolean value. It returns true for the successful prevention of the extensions. Otherwise, returns false.
Exceptions: A TypeError is exception given as the result, when the target is not an Object.
Below example illustrate the Reflect.preventExtensions() method in JavaScript:
Example 1: 
 

javascript




const object1 = {};
 
console.log(Reflect.isExtensible(object1));
Reflect.preventExtensions(object1);
console.log(Reflect.isExtensible(object1));
 
const obj = {}; 
Reflect.preventExtensions(obj); 
console.log( 
Reflect.isExtensible(obj) 
);   
obj.val = 3; 
console.log( 
    obj.hasOwnProperty ("val"
); 

Output: 
 

true
false
false
false

Example 2: 
 

javascript




let empty = {}
console.log(Reflect.isExtensible(empty));
console.log(Reflect.preventExtensions(empty));
console.log(Reflect.isExtensible(empty));
 
const obj = {"val1":3, "val2":4}; 
Reflect.preventExtensions(obj); 
console.log ( obj.hasOwnProperty ( "val1" ) );  
delete obj.val1; 
console.log ( obj.hasOwnProperty ( "val2" ) ); 

Output: 
 

true
true
false
true
true

Supported Browsers: The browsers supported by Reflect.preventExtensions() method are listed below: 
 

  • Google Chrome 49 and above
  • Edge 12 and above
  • Firefox 42 and above
  • Opera 36 and above
  • Safari 10 and above

 

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!