Last active
February 4, 2018 14:42
-
-
Save muffinresearch/3bf28ab2b1dcfa25d1e2 to your computer and use it in GitHub Desktop.
Constructor that works without new keyword.
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 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