Skip to content

Latest commit

History

19 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

construct-new

Like the new operator, but as a function for convenience and familiarity.

Usage

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 bar

What 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 bar

or

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.