Created
December 25, 2013 10:14
-
-
Save smugen/8121942 to your computer and use it in GitHub Desktop.
constructor compatible with be calling as ordinary function
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
// check the prototype | |
function init(args){ | |
if (Object.getPrototypeOf(this) !== init.prototype) { | |
// called without `new` | |
return new init(args); | |
} | |
this.args = args; | |
} | |
// check the constructor | |
function construct(args){ | |
if (Object.getPrototypeOf(this).constructor !== construct) { | |
// called without `new` | |
return new construct(args); | |
} | |
this.args = args; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment