Skip to content

Instantly share code, notes, and snippets.

@eemece2
Forked from mattdesl/disallow-new.js
Created January 26, 2017 20:53
Show Gist options
  • Select an option

  • Save eemece2/3f225322d4c24b6703752c090f91810e to your computer and use it in GitHub Desktop.

Select an option

Save eemece2/3f225322d4c24b6703752c090f91810e to your computer and use it in GitHub Desktop.
avoiding new in classes
// Allows:
// funkyParser()
module.exports = function createFunkyParser(opt) {
return new FunkyParser(opt)
}
function FunkyParser(opt) {
// make params optional
opt = opt || {}
this.foo = opt.foo || 'default'
// handle other options...
...
}
// Allows:
// funkyParser()
// new funkyParser()
module.exports = FunkyParser
function FunkyParser (opt) {
// hide "new"
if (!(this instanceof FunkyParser))
return new FunkyParser(opt)
opt = opt || {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment