Last active
April 16, 2020 14:21
-
-
Save patricksimpson/178f977fcefa0dc4c5fc1061e76d554e to your computer and use it in GitHub Desktop.
JavaScript Design Patterns
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 options = { | |
username: 'blah', | |
server: '127.0.0.1' | |
}; | |
var ConfigObject = (function(params) { | |
var username = params.username || '', | |
server = params.server || '', | |
password = params.password || ''; | |
function _checkPassword() { | |
if (this.password === '') { | |
console.log('no password!'); | |
return false; | |
} | |
return true; | |
} | |
function _checkUsername() { | |
if (this.username === '') { | |
console.log('no username!'); | |
return false; | |
} | |
return true; | |
} | |
function login() { | |
if (_checkPassword() && _checkUsername()) { | |
// perform login | |
} | |
} | |
return { | |
login: login | |
} | |
})(options); |
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
function Hello (greeting) { | |
this.greeting = greeting || 'Hello World!'; | |
} | |
Hello.prototype.speak = function(somethingElse) { | |
var message = somethingElse || this.greeting; | |
console.log(message); | |
} | |
var hi = new Hello('Just saying hi!'); | |
hi.speak(); | |
hi.speak('Something different'); | |
var hello = new Hello(); | |
hello.speak(); | |
hello.speak('Yep'); |
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 GlobalConfigurationObject = (function() { | |
var instance; | |
function createInstance() { | |
return new ConfigObject(); | |
}; | |
var getInstance = function() { | |
if (!instance) { | |
instance = createInstance(); | |
} | |
return instance; | |
} | |
return { | |
getInstance: getInstance | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment