Skip to content

Instantly share code, notes, and snippets.

@patricksimpson
Last active April 16, 2020 14:21
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save patricksimpson/178f977fcefa0dc4c5fc1061e76d554e to your computer and use it in GitHub Desktop.
Save patricksimpson/178f977fcefa0dc4c5fc1061e76d554e to your computer and use it in GitHub Desktop.
JavaScript Design Patterns
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);
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');
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