JavaScript handler.construct() Method

The method handler.construct serves to intercept the creation of new instances. This function yields an object as its output.

Syntax

Example

construct: function(target, argumentsList, newTarget)

Parameters

Target : The target object.

argumentsList : List of the constructor.

newTarget: The constructor that was initially invoked, represented by p above.

Return value

This method returns an object.

Browser Support

Chrome 49
Edge 12
Firefox 18
Opera 36

Example 1

Example

var B= function(text) {

  this.text = text;

};

var Button1 = new Proxy(B, {

  construct: function(target, parameters) {

document.writeln('Java Script');



    var button= Object.create(target.prototype);

    target.apply(button, parameters);

    return button;

  }

});



var button = new Button1('proxy Constructor');

document.writeln(button.text);

Output:

Output

Java Script proxy Constructor

Example 2

Example

var pro = new Proxy(function()

                    {}, {

  construct: function(objTarget, args, oldConstructor) {

     return { Value : args[0] + " to anybody" }	

  }

})



document.write(JSON.stringify(new pro("Hello "), null, ' '))

Output:

Output

{ "Value": "Hello to anybody" }

Example 3

Example

function M(val) {

  this.val = val;

}



const N = {

  construct(target, args) {

  document.writeln('Constructor called');

  return new target(...args);

  }

};

const proxy= new Proxy(M, N)

document.writeln(new proxy('Proxi').val);

Output:

Output

Constructor called Proxi

Input Required

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