Created
January 20, 2012 23:11
-
-
Save QGao/1650137 to your computer and use it in GitHub Desktop.
A n ice singleton patten sample from Rick
This file contains hidden or 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
/** | |
* A singleton object | |
* | |
* Singleton objects are useful but use them sparingly. By using singletons | |
* you are creating dependencies in your software that might not be necessary. | |
* If you find yourself using this over and over again you probably need | |
* to rethink what you're doing. Remember, in general, globals are bad. | |
*/ | |
var Preferences = { | |
fbToken: '1234', | |
twToken: '1234', | |
userData: {}, | |
updateUser: function() {}, | |
nukeProject: function() {} | |
}; | |
/** | |
* Alternative way of creating a singleton | |
* using revealing module pattern | |
*/ | |
var Preferences = (function() { | |
var fbToken = '1234', | |
twToken = '1234', | |
userData = {}, | |
updateUser = function() {}, | |
nukeProject = function() {}; | |
return { | |
fbToken: fbToken, | |
twToken: twToken, | |
userData: userData, | |
updateUser: updateUser, | |
nukeProject: nukeProject | |
} | |
})(); | |
/** | |
* Alternative way of creating a singleton | |
* using normal module pattern | |
*/ | |
var Preferences = (function() { | |
var api = {}; | |
api.fbToken = '1234'; | |
api.twToken = '1234'; | |
api.userData = {}; | |
api.updateUser = function() {}; | |
api.nukeProject = function() {}; | |
return api; | |
})(); | |
console.log( Preferences ); | |
console.log( Preferences.fbToken ); | |
console.log( Preferences.twToken ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment