Last active
August 29, 2015 14:04
-
-
Save MoOx/b2551b4b404d4d4bee6b to your computer and use it in GitHub Desktop.
Constructor pattern to allow both new and functional usage
This file contains 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
function Ctor(options) { | |
if(!(this instanceof Ctor)) { // same as (!Ctor.prototype.isPrototypeOf(this)) | |
return Ctor.apply(Object.create(Ctor.prototype), arguments) | |
} | |
// ... | |
return this | |
} |
This file contains 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
var Ctor = require("ctor") | |
var instance = new Ctor({/*...*/}) |
This file contains 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
var ctor = require("ctor") | |
var instance = ctor({/*...*/}) |
bloodyowl
commented
Aug 6, 2014
Updated according to your comment.
Thanks !
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment