Reflect.preventExtensions() JavaScript

The JavaScript Reflect.preventExtensions() method prevents any future extensions to an object i.e. prevents add and modification of properties. It is It is similar to Object.preventExtensions().

Syntax:

Reflect.preventExtensions(target)

Parameters:
target: It represents the object which to prevent extensions.

Return:
It returns true if the target object was successfully set to prevent extensions otherwise returns false.

Note: It throws TypeError, if target is not an Object.

Reflect.preventExtensions() VS Object.preventExtensions()

Reflect.preventExtensions(31);
// It throws TypeError: 31 is not an object

Object.preventExtensions(31);
// It returns 31

Example 1:

<!DOCTYPE html>
<html>
<body>
<script>
const obj = {alpha: 100, beta: 200, gamma: 300};  
document.write( Reflect.isExtensible (obj) + "<br>");   
Reflect.preventExtensions (obj);  
document.write( Reflect.isExtensible (obj) );   
</script>
</body>
</html>

Output:

true

Example 2:

<!DOCTYPE html>
<html>
<body>
<script>
 
var object1 = {};
 
document.write(Reflect.isExtensible(object1));
document.write("</br>");
Reflect.preventExtensions(object1);
document.write(Reflect.isExtensible(object1));
</script>
</body>
</html>

Output:

true
false
Please Share