Like the new operator, but as a function for convenience and familiarity.
What you would normally do:
classFoo{constructor(name){this.name=name}print(){console.log("Hi, I am "+this.name)}}constmyFoo=newFoo("bar")myFoo.print()// output: Hi, I am barWhat you would do with this:
constconstruct=require('construct-new')classFoo{constructor(name){this.name=name}print(){console.log("Hi, I am "+this.name)}}constmyFoo=construct({target: Foo,args: ["bar"]})myFoo.print()// Hi, I am baror
construct({target: Foo,args: ["bar"],callback: (myFoo)=>{myFoo.print()// Hi, I am bar}})If the class doesn't take any arguments, you don't have to pass in the args property, it will still work like the args is an empty array.