Skip to content

10xEngineersQualityProgramming/construct-new

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published