Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@muffinresearch
Last active February 4, 2018 14:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save muffinresearch/3bf28ab2b1dcfa25d1e2 to your computer and use it in GitHub Desktop.
Save muffinresearch/3bf28ab2b1dcfa25d1e2 to your computer and use it in GitHub Desktop.
Constructor that works without new keyword.
function MyConstructor(foo){
if (!(this instanceof MyConstructor)){
return new MyConstructor(foo);
}
this.foo = foo;
return this;
}
var instance1 = MyConstructor('foo1');
var instance2 = new MyConstructor('foo2');
console.log(instance1.foo);
console.log(instance2.foo);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment