CodesJava

Easy learning with example program codes

Reflect.defineProperty() JavaScript

The JavaScript Reflect.defineProperty() adds or modifies a property of an object. It is same as Object.defineProperty() but it returns a Boolean.

Syntax:

Reflect.defineProperty(target, propertyKey, attributes) 

Parameters:
target: It represents the object that have to be invoked.
propertyKey: It represents the name of the property to be defined or modified.
attributes: It represents attributes for the property being defined or modified.

Return:
It returns true if the new property is successfully added or existing property is modify successfully otherwise returns false.

Example 1:

<!DOCTYPE html>
<html>
<body>
<script>
const obj = {};  
(Reflect.defineProperty(obj, 'prop', {value: 100}))  
document.write(obj.prop);  
</script>
</body>
</html>

Output:

100

Example 2:

<!DOCTYPE html>
<html>
<body>
<script>
const object1 = {};
 
if (Reflect.defineProperty(object1, 'property1', {value: 142})) {
  document.write('property1 created successfully');
} else {
  document.write('Error in creating property1');
}
document.write("</br>");
document.write(object1.property1);
</script>
</body>
</html>

Output:

property1 created successfully
142
Please follow and like us:
error








Copyright © 2019 CodesJava DMCA.com Protection Status