Last active
May 8, 2018 02:46
-
-
Save alvarezmauro/6336f5160b19b1531e456df3cf31de97 to your computer and use it in GitHub Desktop.
JS Design Pattern - Singleton
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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