JavaScript handler.getOwnPropertyDescriptor() Method

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: