Skip to content

Instantly share code, notes, and snippets.

@plumpNation
Created November 2, 2013 14:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plumpNation/7279589 to your computer and use it in GitHub Desktop.
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.
var YourObject = function () {
// ensure YourCobject was called as a constructor
if (!(this instanceof YourObject)) {
return new YourObject();
}
// do your thing now, it's safe!!
};
@plumpNation
Copy link
Author

Now I believe in throwing an Error instead and using a linter to yell at you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment