Saturday, December 28, 2024
Google search engine
HomeLanguagesJavascriptJavaScript Reflect getOwnPropertyDescriptor() Method

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 mentioned above and described below:

  • Obj: This parameter holds the target object and it looks for the property.
  • Key: This parameter is used to get its own property descriptor for the name of the property.

Return value: This method returns the property descriptor object and returns undefined if the target object does not exist.

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

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

Example 1: In this example, we will get the descriptor of an object using Reflect.getOwnPropertyDescriptor() method in JavaScript.

javascript




let object = {
    property1: "neveropen"
};
 
console.log(Reflect.getOwnPropertyDescriptor(
    object, 'property1').value);
 
console.log(Reflect.getOwnPropertyDescriptor(
    object, 'property2'));
 
console.log(Reflect.getOwnPropertyDescriptor(
    object, 'property1').writable);
 
let object1 = {
    property2: "Javascript"
};
 
const object3 = {
    property3: 232
};
 
console.log(Reflect.getOwnPropertyDescriptor(
    object1, 'property2').value);
 
console.log(Reflect.getOwnPropertyDescriptor(
    object1, 'property3'));
 
console.log(Reflect.getOwnPropertyDescriptor(
    object1, 'property2').writable);
 
console.log(Reflect.getOwnPropertyDescriptor
    (object3, "null") === undefined
);


Output

neveropen
undefined
true
Javascript
undefined
true
true

Example 2: In this example, we will get the descriptor of an object using the Reflect.getOwnPropertyDescriptor() method in JavaScript.

javascript




const object1 = {
    property1: "NULL"
};
 
console.log(Reflect.getOwnPropertyDescriptor(
    object1, 'property2'));
console.log(Reflect.getOwnPropertyDescriptor(
    object1, 'property1').writable);
console.log(Reflect.getOwnPropertyDescriptor(
    object1, 'property1'));
 
console.log(Reflect.getOwnPropertyDescriptor(
    { val: 'hello' }, 'val'));
console.log(Reflect.getOwnPropertyDescriptor(
    { val1: 'hello' }, 'y'));
console.log(Reflect.getOwnPropertyDescriptor(
    [], 'length'));


Output

undefined
true
{ value: 'NULL', writable: true, enumerable: true, configurable: true }
{
  value: 'hello',
  writable: true,
  enumerable: true,
  configurable: true
}
undefined
{ value: 0, writable: ...

Example 3: Calling the non-object and an error occurs.

javascript




console.log(Reflect.getOwnPropertyDescriptor('foo', 0));


Output:

Error: Reflect.getOwnPropertyDescriptor called on non-object

Supported Browsers:

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

RELATED ARTICLES

Most Popular

Recent Comments