Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

javascript
property
modify
add
communitycreator

What is Reflect.defineProperty in JavaScript?

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Reflect.defineProperty is used to add or modify the property of an object.

Syntax


Reflect.defineProperty(targetObj, propertyKey, descriptor);

Arguments

  • targetObj: The object in which the property is to be defined or modified.

  • propertyKey: The name of the property to be defined or modified.

  • descriptor: The value of the property descriptor that is to be set. We can use the descriptor to define or update the properties below.

    • value: The value associated with the property.

    • writable: A Boolean value that denotes if the value of the property can be changed. The default value is false.

    • enumerable: A Boolean value that denotes if the property occurs in the enumeration of the properties on the object. The default value is false.

    • configurable: A Boolean value that denotes if the property descriptor of the object can be changed. The default value is false.

    • get: Getter function for the property.

    • set: Setter function for the property.

Return value

This method returns true if the value is successfully defined.

Code

Example 1

Set the value of the property of an object.

let obj = {a : 10};
let isUpdated = Reflect.defineProperty(obj, 'a', {value: 20});
console.log("isUpdated :", isUpdated);
console.log(obj.a);

Explanation

In the code above, we created an object with the property a and with the value 10. Then, we called:


Reflect.defineProperty(obj, 'a', {value: 20});

to change the value of the property a to 20. This will update the value of the property a.

Example 2

Make the value of the property unchangeable.

let obj = {a : 10};
let isUpdated = Reflect.defineProperty(obj, 'a', {writable: false});
console.log("isUpdated :", isUpdated);
obj.a = 20;
console.log(obj.a);

Explanation

In the code above, we created an object with the property a and with the value 10. Then, we called:


Reflect.defineProperty(obj, 'a', {writable: false});

to make the property a unchangeable. Then, we tried to change the value of the property a, but the value will not be updated because the value of the property a is unchangeable.

Example 3

Define a new property in an object.

let obj = {a : 10};
let isDefined = Reflect.defineProperty(obj, 'b', {value: 20, writable: true, enumerable: true, configurable: true});
console.log("isDefined :", isDefined);
console.log(obj.b);

Explanation

We created an object with the property a and with the value 10 in the code above. Then we defined a new property b using:


Reflect.defineProperty(obj, 'b', {value: 20, writable: true, enumerable: true, configurable: true});

This will add a new property in the obj object.

RELATED TAGS

javascript
property
modify
add
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring