Skip to content

Instantly share code, notes, and snippets.

@jlord
Created January 26, 2012 05:57
Show Gist options
  • Save jlord/1681274 to your computer and use it in GitHub Desktop.
Save jlord/1681274 to your computer and use it in GitHub Desktop.
A really useful function, or Jessica learns about object prototypes
var food = function(type) {
function changeType(newType) {
type = newType
}
function judgment() {
if (type === "bread" || type === "olives") {
return "Awesome tastes"
} else {
return "Sucky crappy tastes, man"
}
}
return {
changeType: changeType,
judgment: judgment
}
}
var bread = food('bread')
console.log(bread.judgment())
bread.changeType('taco')
console.log(bread.judgment())
function Food(type) {
this.foodType = type
}
Food.prototype.judgement = function() {
if (this.foodType === "bread" || this.foodType === "olives") {
return "Awesome tastes"
} else {
return "Sucky crappy tastes, man"
}
}
Food.prototype.changeType = function(newType) {
this.foodType = newType
}
var bread = new Food("bread")
var cheese = new Food("mozz")
console.log(bread.judgement())
bread.changeType("Pizza")
console.log(bread.judgement())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment