Reflect.preventExtensions() JavaScript

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

Syntax:

Reflect.preventExtensions(target)

Parameters: target: It represents the object that prevents extensions.

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

Note: It throws TypeError if the 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>

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>
Please follow and like us:
Content Protection by DMCA.com