Zezura
Zezura

Reputation: 126

function returning constructor and calling directly

I'm currently coding in Javascript and calling a function that returns a constructor, I would like to immediately create the object like this. but I does not work. because I want this code in one line instead of having to make another var to initiate it. Thanks in advance.

this.vendor = new Lib.vendorAPI.retreiveVendor(this.vendorConst)(this);

this way it works, but its not inline.

var currentVendor = new Lib.vendorAPI.retreiveVendor(this.vendorConst)(this);
this.vendor = new currentVendor(this);

thanks in advance.

Upvotes: 0

Views: 39

Answers (2)

Matt
Matt

Reputation: 4752

What about

var currentVendor = new new Lib.vendorAPI.retreiveVendor(this.vendorConst)(this);

Upvotes: 0

Zezura
Zezura

Reputation: 126

found the answer myself: adding brackets around the function will tell the browser that it first needs to execute the function and then create the instance from the returned constructor. like this:

this.vendor = new (Lib.vendorAPI.retreiveVendor(this.vendorConst))(this);

Upvotes: 1

Related Questions