Skip to content

Instantly share code, notes, and snippets.

@yuya-takeyama
Created August 7, 2010 02:36
Show Gist options
  • Save yuya-takeyama/512354 to your computer and use it in GitHub Desktop.
Save yuya-takeyama/512354 to your computer and use it in GitHub Desktop.
Singleton Object in JavaScript
var Singleton = (function () {
var
obj = {},
id,
init,
get_instance,
get_id;
init = function () {
id = Math.random();
};
obj.get_instance = function () {
if (typeof id === 'undefined') {
init();
}
return obj;
};
obj.get_id = function () {
return id;
};
return obj;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment