Skip to content

Instantly share code, notes, and snippets.

@ncrohn
Created August 12, 2012 17:53
Show Gist options
  • Save ncrohn/3333387 to your computer and use it in GitHub Desktop.
Save ncrohn/3333387 to your computer and use it in GitHub Desktop.
Myself as a JavaScript Class
var NickCrohn = function(world) {
var firstName = 'Nicholas',
middleName = 'William',
lastName = 'Crohn',
favoriteFood = {
name: 'Lasagna',
isFood: true,
isDrink: false
},
favoriteDrink = {
name: 'Coffee',
isFood: false,
isDrink: true
},
favoriteAlcoholicDrink = {
name: 'Beer',
isFood: false,
isDrink: true
};
function speak(words) {
console.log('%s says, "%s"', firstName, words);
}
function consume(consumable) {
if(consumable.isDrink) {
//openMouth();
//swallow();
speak('Ahhhhh, ' + consumable.name);
} else if(consumable.isFood) {
//openMouth();
//chew();
//swallow();
speak('Omnomnom, ' + consumable.name);
}
}
return {
getFullName: function() {
var fullName = [firstName, middleName, lastName];
return fullName.join(' ');
},
eat: function(food) {
if(!food) {
food = favoriteFood;
}
consume(food);
},
drink: function(drink) {
if(!drink) {
drink = favoriteDrink;
var date = new Date();
if(date.getHours() > 12) {
drink = favoriteAlcoholicDrink;
}
}
consume(drink);
}
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment