Skip to content

Instantly share code, notes, and snippets.

@frontdevops
Created August 25, 2016 08:19
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 frontdevops/4b8a8768fe1d3916e96cead01cde74a6 to your computer and use it in GitHub Desktop.
Save frontdevops/4b8a8768fe1d3916e96cead01cde74a6 to your computer and use it in GitHub Desktop.
(function(g){
/** @type {SingletonConstructor} */
var instance;
g.Singleton = function() {
if (instance !== void 0) return instance;
return instance = this;
};
g.Singleton.prototype.foo = 123;
})(window||global);
var obj1 = new Singleton;
var obj2 = new Singleton;
console.log(obj2.foo === 123);
obj1.foo = 456;
console.log(obj2.foo === 456);
console.log(obj1 === obj2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment