Skip to content

Instantly share code, notes, and snippets.

@alvarezmauro
Last active May 8, 2018 02:46
Show Gist options
  • Save alvarezmauro/6336f5160b19b1531e456df3cf31de97 to your computer and use it in GitHub Desktop.
Save alvarezmauro/6336f5160b19b1531e456df3cf31de97 to your computer and use it in GitHub Desktop.
JS Design Pattern - Singleton
var singleton = (function () {
var singletonInstance;
function create () {
var singletonPrivateVariable = 'Private variable'
function singletonMethod() {
// Method
}
function otherSingletonMethod() {
//Method
}
return {
singletonProperty: 'singletonProperty',
otherSingletonProperty: 'otherSingletonProperty',
otherSingletonMethod: singletonMethod,
otherSingletonMethod: otherSingletonMethod
};
}
return {
getInstance: function() {
if(!singletonInstance) {
singletonInstance = create();
}
return singletonInstance;
}
};
})();
var singletonObj = singleton.getInstance();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment