construct-new
Like the new operator, but as a function for convenience and familiarity.
Usage
What you would normally do:
class Foo {
constructor(name) {
this.name = name
}
print() {
console.log("Hi, I am " + this.name)
}
}
const myFoo = new Foo("bar")
myFoo.print()
What you would do with this:
const construct = require('construct-new')
class Foo {
constructor(name) {
this.name = name
}
print() {
console.log("Hi, I am " + this.name)
}
}
const myFoo = construct({
target: Foo,
args: ["bar"]
})
myFoo.print()
or
construct({
target: Foo,
args: ["bar"],
callback: (myFoo) => {
myFoo.print()
}
})
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.