-
-
Save jfkhoury/d52d55e17622a4b3fa0bd1a2c3922146 to your computer and use it in GitHub Desktop.
avoiding new in classes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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... | |
... | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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