Wednesday, July 3, 2024
HomeLanguagesJavascriptJavaScript Reflect defineProperty() Method

JavaScript Reflect defineProperty() Method

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, 'neveropen1', { value: 42 })) {
    console.log('neveropen1 assigned');
 
} else {
    console.log('problem created by  neveropen1');
}
 
console.log(object1.neveropen1);
 
const object2 = {};
const object3 = {};
(Reflect.defineProperty(object2, 'neveropen2', { value: 97 }))
if (Reflect.defineProperty(object3, 'neveropen3', { value: 23 })) {
    console.log('neveropen3 assigned');
} else {
    console.log('problem created by neveropen3');
}
console.log(object3.neveropen3);
console.log(object2.neveropen2);


Output

neveropen1 assigned
42
neveropen3 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.

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

Calisto Chipfumbu
Calisto Chipfumbuhttp://cchipfumbu@gmail.com
I have 5 years' worth of experience in the IT industry, primarily focused on Linux and Database administration. In those years, apart from learning significant technical knowledge, I also became comfortable working in a professional team and adapting to my environment, as I switched through 3 roles in that time.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments