JavaScript | Reflect.getOwnPropertyDescriptor() Method

The Reflect.getOwnPropertyDescriptor() method in Javascript is used to get the descriptor of an 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 accept 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 an own property descriptor for the name of the property.

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

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



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

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
  
let object = {
    property1: "geeks"
};
  
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 
);
  
</script>

chevron_right


Output:

"geeks"
undefined
true
"Javascript"
undefined
true
true

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
  
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'));
  
</script>

chevron_right


Output:]

undefined
true
[object Object] {
  configurable: true,
  enumerable: true,
  value: "NULL",
  writable: true
}
[object Object] {
  configurable: true,
  enumerable: true,
  value: "hello",
  writable: true
}
undefined
[object Object] {
  configurable: false,
  enumerable: false,
  value: 0,
  writable: true
}

Example 3: Calling the non-object and error occured.

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
console.log(Reflect.getOwnPropertyDescriptor('foo', 0));
</script>

chevron_right


Output:

Error: Reflect.getOwnPropertyDescriptor called on non-object

Supported Browsers: The browsers supported by JavaScript Reflect.getOwnPropertyDescriptor() Method are listed below:

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Opera
  • Safari
  • Edge

Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyDescriptor

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.