Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

JavaScript | handler.defineProperty() Method

  • Last Updated : 27 Sep, 2021

The handler.defineProperty() method in Javascript is used to define the new properties and to modify the existing properties directly on an object. It is trap for Object.defineProper().

const p = new Proxy(target, {
  defineProperty: function(target, property, descriptor) {

Parameters: This method accepts three 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 holds the target object.
  • property: This parameter is the name or Symbol of the property whose description is going to be retrieved..
  • descriptor: This parameter is the descriptor for the property being defined or modified.

Return value: This method returns a Boolean value which is used to indicate if the property is successfully defined.
Below example illustrate the handler.defineProperty() method in JavaScript:
Example 1: 


const p = new Proxy({}, {
  defineProperty: function(target, prop, descriptor) {
    console.log('Type : ' + prop);
    return true;
const desc = { configurable: true, enumerable: true, value: 10 };
Object.defineProperty(p, 'String', desc);
var xyz = {}; 
var proxy = new Proxy(xyz, { 
  defineProperty: function(target, name, propertyDescriptor) { 
console.log('in defineProperty'); 
return Object.defineProperty(target, name, propertyDescriptor); 
Object.defineProperty(proxy, 'bar', {} ); 


"Type : String"
"in defineProperty"

Example 2: 


const handler1 = {
  defineProperty(target, key, descriptor) {
    invariant(key, 'define');
    return true;
function invariant(key, action) {
  if (key[0] === '_') {
    throw new Error(
      `Invalid attempt to ${action} private "${key}" property`);
const monster1 = {};
const proxy1 = new Proxy(monster1, handler1);
console.log(proxy1._propt = 'Handler defineProperty');


Error: Invalid attempt to define private "_propt" property

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

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

My Personal Notes arrow_drop_up
Recommended Articles
Page :