Open In App

JavaScript Reflect defineProperty() Method

Last Updated : 07 Aug, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

JavaScript Reflect.defineProperty() method in JavaScript is used to allow the precise addition to or modification of a property on an object. This method returns a Boolean value which indicates whether the property was successfully defined.

Syntax:

Reflect.defineProperty(target, propertyKey, attributes) 

Parameter: This method accepts three parameters as mentioned above and described below:

  • target: This parameter defines the property and the target object.
  • propertyKey: This parameter is the name of the property which is to be defined or modified.
  • Attributes: This parameter is the attributes for the property which is being defined or modified.

Return value: This method returns a Boolean value which indicates whether the property was successfully defined.

Exceptions: A TypeError is an exception given as the result when the target is not an Object.

Below examples illustrate the Reflect.defineProperty() method in JavaScript:

Example 1: The below example shows the basic use of the Reflect.defineProperty() method in JavaScript.

javascript




const object1 = {};
 
if (Reflect.defineProperty(object1, 'geeks1', { value: 42 })) {
    console.log('geeks1 assigned');
 
} else {
    console.log('problem created by  geeks1');
}
 
console.log(object1.geeks1);
 
const object2 = {};
const object3 = {};
(Reflect.defineProperty(object2, 'geeks2', { value: 97 }))
if (Reflect.defineProperty(object3, 'geeks3', { value: 23 })) {
    console.log('geeks3 assigned');
} else {
    console.log('problem created by geeks3');
}
console.log(object3.geeks3);
console.log(object2.geeks2);


Output

geeks1 assigned
42
geeks3 assigned
23
97

Example 2: The below example shows the basic use of the Reflect.defineProperty() method in JavaScript.

javascript




const a = {};
const result = Reflect.defineProperty(a, "geek1",
    { value: 19, });
 
console.log(a);
console.log(result);
 
const b = {};
const result1 = Reflect.defineProperty(b, "geek2",
    {
        value: 56,
        writable: false
    }
);
console.log(b);
console.log(result1);
 
let obj = {}
Reflect.defineProperty(obj, 'x', { value: 71 })  // true
console.log(obj.x)


Output

{}
true
{}
true
71

Supported Browsers:

The browsers are supported by JavaScript Reflect.defineProperty() 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

We have a complete list of Javascript Reflects methods, to check those go through the JavaScript Reflect Reference article.



Previous Article
Next Article

Similar Reads

JavaScript Handler defineProperty() Method
JavaScript handler.defineProperty() method in Javascript is used to define the new properties and to modify the existing properties directly on an object. It is a trap for Object.defineProper(). Syntax: const p = new Proxy(target, { defineProperty: function(target, property, descriptor) { } }); Parameters: This method accepts three parameters as me
2 min read
JavaScript Object defineProperty() Method
The Object.defineProperty() method in JavaScript is a Standard built-in object which defines a new property directly on an object or it can also modify the existing property of an object and returns the object. Syntax: Object.defineProperty(obj, prop, descriptor) Parameter: This method accepts three parameters as mentioned above and described below
2 min read
What is Object.defineProperty() in JavaScript?
In JavaScript, Object.defineProperty() is a method used to define a new property directly on an object or modify an existing property on an object. It provides fine-grained control over property attributes such as value, writable, enumerable, and configurable. Syntax:Object.defineProperty(object, propertyKey, descriptor)Parameters:object: The objec
1 min read
JavaScript Reflect apply() Method
Javascript Reflect.apply() method is a standard build-in object in JavaScript which is used to call a function using the specified argument. It works similar to the Function.prototype.apply() method to call a function, but in an efficient manner and easy to understand. Syntax: Reflect.apply(target, thisArgument, argumentsList) Parameters: This meth
2 min read
JavaScript Reflect get() Method
JavaScript Reflect.get() method in JavaScript is used to allow users to get the property from an object as a function. This method always returns the value of the property. Syntax: Reflect.get(target, propertyKey, receiver) Parameters: This method accepts three parameters as mentioned above and described below: target: This parameter is used to get
2 min read
JavaScript Reflect deleteProperty() Method
JavaScript Reflect.deleteProperty() method in JavaScript is used to delete a property on an object. It returns a Boolean value which indicates whether the property was successfully deleted. Syntax: Reflect.deleteProperty( target, propertyKey ) Parameters: This method accepts two parameters as mentioned above and described below: target: This parame
2 min read
JavaScript Reflect getPrototypeOf() Method
JavaScript Reflect.getPrototypeOf() method in JavaScript is used to return the prototype of the specified object. Syntax: Reflect.getPrototypeOf( obj ) Parameters: This method accepts a single parameter as mentioned above and described below: obj: This parameter is the target object and it is used to get the prototype.Return value: This method is u
2 min read
JavaScript Reflect has() Method
JavaScript Reflect.has() method in JavaScript is used to check whether the property exists in an object or not. It works like the in operator as a function. Syntax: Reflect.has(target, propertyKey) Parameters: This method accepts two parameters as mentioned above and described below: target: This parameter is the target object and it looks for the
2 min read
JavaScript Reflect isExtensible() Method
JavaScript Reflect.isExtensible() method in JavaScript is used to check whether an object is extensible or not(Checks whether other properties can be added to it or not). This method is similar to Object.isExtensible() but it will cause a TypeError if the target is not an object. Syntax: Reflect.isExtensible( obj )Parameters: This method accepts a
2 min read
JavaScript Reflect getOwnPropertyDescriptor() Method
JavaScript Reflect.getOwnPropertyDescriptor() method in Javascript is used to get the descriptor of an object if it exists in the object. It is the same as the Object.getOwnPropertyDescriptor method, but non-object targets are handled differently. Syntax: Reflect.getOwnPropertyDescriptor(obj, Key) Parameters: This method accepts two parameters as m
2 min read