Skip to content

Instantly share code, notes, and snippets.

@mateuszkocz
Created December 1, 2013 12:13
Show Gist options
  • Save mateuszkocz/7732912 to your computer and use it in GitHub Desktop.
Save mateuszkocz/7732912 to your computer and use it in GitHub Desktop.
Safe constructor.
var Constructor = function() {
if (!(this instanceof Constructor)) {
return new Constructor();
}
// Properties and methods...
this.something = 'something';
// ...
}
var object1 = new Constructor(),
object2 = Constructor();
console.log(object1.something); // 'something'
console.log(object2.something); // 'something'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment