Skip to content

Instantly share code, notes, and snippets.

@smugen
Created December 25, 2013 10:14
Show Gist options
  • Save smugen/8121942 to your computer and use it in GitHub Desktop.
Save smugen/8121942 to your computer and use it in GitHub Desktop.
constructor compatible with be calling as ordinary function
// 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