Last active
December 27, 2015 01:59
-
-
Save haeric/7248868 to your computer and use it in GitHub Desktop.
This file contains 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 LolCat () { | |
// Private variabler og metoder | |
var foods = ['cheezburgerz', 'icecriim', 'your dinner']; | |
function likes (food) { | |
return foods.indexOf(food) !== -1; | |
} | |
// Såkalte priviliged variabler og metoder: De har tilgang til private variabler, men kan brukes utenfra | |
// Disse kan også overskrives med nye funksjoner, men de vil da ikke ha tilgang til de samme private variablene | |
this.happiness = 0.0; | |
this.feed = function (food) { | |
if (likes(food)) { | |
this.happiness++; | |
} | |
else { | |
this.happiness--; | |
} | |
} | |
} | |
// Public variabler og metoder | |
// Kan ikke se noen private variabler, men kan se priviliged | |
LolCat.prototype.pet = function () { | |
this.happiness += Math.random() * 2 - 1; | |
} | |
var pusur = new LolCat(); | |
pusur.foods; // Finnes ikke, du kan da aldri få vite hva katten din faktisk liker... | |
pusur.feed("cheezburgerz"); | |
pusur.happiness; // 1 | |
pusur.pet(); | |
pusur.happiness; // Who knows!? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment