1.0.1 • Published 9 months ago

construct-new v1.0.1

Weekly downloads
-
License
MIT
Repository
-
Last release
9 months ago

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() // output: Hi, I am bar

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() // 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.