Skip to content

Instantly share code, notes, and snippets.

@chaicko
Forked from mattdesl/disallow-new.js
Created April 15, 2016 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chaicko/b9aa3b3eb0954dd41c20a6c9e2bb8c08 to your computer and use it in GitHub Desktop.
Save chaicko/b9aa3b3eb0954dd41c20a6c9e2bb8c08 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