JavaScript handler.getOwnPropertyDescriptor() Method - JavaScript Tutorial

JavaScript handler.getOwnPropertyDescriptor() Method

BLUF: This tutorial on JavaScript handler.getOwnPropertyDescriptor() Method provides an in-depth look at JavaScript's core features. It includes practical examples and code snippets to help you master modern JS development.
Key Discovery: JavaScript handler.getOwnPropertyDescriptor() Method

Understanding JavaScript handler.getOwnPropertyDescriptor() Method is crucial for building dynamic, interactive web applications. Explore the examples below to see it in action.

The handler.getOwnPropertyDescriptor function serves as a trap for the Object.getOwnPropertyDescriptor method. This function is defined as a property of the target object, which itself cannot be extended.

Syntax

Example

getOwnPropertyDescriptor: function(target, prop)

Parameters

Target : The target object.

Prop: The identifier of the property for which the details are to be obtained.

Return value

This method returns an object or undefined.

Browser Support

Chrome 49
Edge 12
Firefox 18
Opera 36

Example 1

Example

const proxy = new Proxy({}, {

  getOwnPropertyDescriptor: function(target, name) {

  document.writeln('Java Script is a scripting language');

    //expected output: Java Script is a scripting language

 return Object.getOwnPropertyDescriptor(target, name);

  }

});

console.dir(Object.getOwnPropertyDescriptor(proxy, 'foo'));

Output:

Output

Java Script is a scripting language

Example 2

Example

const r={}

const p = new Proxy(r, {

  getOwnPropertyDescriptor: function(target, prop) {

  document.write('Value : ' + prop);

    return { configurable: true, enumerable: true, value: 10 };

  }

});

document.writeln(Object.getOwnPropertyDescriptor(p, 'abc ').value);

Output:

Output

Value : abc 10

Example 3

Example

var a = {

  eyeCount: 3

}



var b = {

  getOwnPropertyDescriptor(target, prop) {

    

   document.writeln(`Java Script proxt : ${prop}`);

    

    

    return { configurable: true, value: 50};

  }

};

const c= new Proxy(a, b);

document.writeln(Object.getOwnPropertyDescriptor(c, 'Own property').value);

Output:

Output

Java Script proxt : Own property 50

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience