Created
January 26, 2012 05:57
-
-
Save jlord/1681274 to your computer and use it in GitHub Desktop.
A really useful function, or Jessica learns about object prototypes
This file contains hidden or 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 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()) |
This file contains hidden or 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 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