Skip to content
Related Articles

Related Articles

Improve Article

JavaScript | handler.getOwnPropertyDescriptor() Method

  • Last Updated : 28 Sep, 2021

The handler.getOwnPropertyDescriptor() method in Javascript is a trap for Object.getOwnPropertyDescriptor() method. A property cannot be reported as non-existent, if it exists as a non-configurable own property of the target object.
Syntax: 
 

const p = new Proxy(target, {
  getOwnPropertyDescriptor: function(target, prop) {
  }
});

Parameters: This method accept two parameters as mentioned above and described below: 
 

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

  • Target: This parameter is the target object.
  • Prop: This parameter is the name of the property whose description should be retrieved.

Return value: This method returns an object or undefined.
Below examples illustrate the handler.getOwnPropertyDescriptor() method in JavaScript:
Example 1: 
 

javascript




<script>
const monster1 = {
  num: 4
};
 
const handler1 = {
  getOwnPropertyDescriptor(target, prop) {
    console.log(`Type : ${prop}`);
 
    return { configurable: true, enumerable: true, value: 5 };
  }
};
 
const proxy1 = new Proxy(monster1, handler1);
 
console.log(Object.getOwnPropertyDescriptor(proxy1, 'num').value);
console.log(Object.getOwnPropertyDescriptor(proxy1, 'bool').enumerable);
</script>

Output: 
 



> "Type : num"
> 5
> "Type : bool"
> true

Example 2: 
 

javascript




<script>
const p = new Proxy({ VAL: 20}, {
  getOwnPropertyDescriptor: function(target, prop) {
    console.log('Property : ' + prop);
    return { configurable: true, enumerable: true, value: 10 };
  }
});
 
console.log(Object.getOwnPropertyDescriptor(p, 'VAL').value);
 
const obj = { a: 10 };
Object.preventExtensions(obj);
const pval = new Proxy(obj, {
  getOwnPropertyDescriptor: function(target, prop) {
    return undefined;
  }
});
 
console.log(Object.getOwnPropertyDescriptor(pval));
</script>

Output: 
 

> "Property : VAL"
> 10
> undefined

Supported Browsers: The browsers supported by handler.getOwnPropertyDescriptor() method are listed below: 
 

  • Google Chrome 49 and above
  • Edge 12 and above
  • Firefox 18  and above
  • Opera 36 and above
  • Safari 10 and above

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :