Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.