Skip to content

Instantly share code, notes, and snippets.

@andhapp
Forked from jasonwyatt/MySingleton.js
Created April 4, 2014 16:26
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 andhapp/9978175 to your computer and use it in GitHub Desktop.
Save andhapp/9978175 to your computer and use it in GitHub Desktop.
define(function(){
var instance = null;
function MySingleton(){
if(instance !== null){
throw new Error("Cannot instantiate more than one MySingleton, use MySingleton.getInstance()");
}
this.initialize();
}
MySingleton.prototype = {
initialize: function(){
// summary:
// Initializes the singleton.
this.foo = 0;
this.bar = 1;
}
};
MySingleton.getInstance = function(){
// summary:
// Gets an instance of the singleton. It is better to use
if(instance === null){
instance = new MySingleton();
}
return instance;
};
return MySingleton.getInstance();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment