Created
November 2, 2013 14:42
-
-
Save plumpNation/7279589 to your computer and use it in GitHub Desktop.
Reading the code in the wonderful Lawnchair library by Brian Leroux, the first thing I see is this great little pattern. All it does is make damn sure that 'new' is used to instantiate the object, as it must not be used as a Singleton.
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 YourObject = function () { | |
// ensure YourCobject was called as a constructor | |
if (!(this instanceof YourObject)) { | |
return new YourObject(); | |
} | |
// do your thing now, it's safe!! | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Now I believe in throwing an Error instead and using a linter to yell at you.