Last active
December 27, 2015 01:59
-
-
Save haeric/7248568 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 UnfortunateKitty (name) { | |
// Private variabler og funksjoner, bare tilgjengelig i dette scopet | |
var numberOfLives = 9; | |
function die () { | |
console.log("Honey, the cat died :("); | |
} | |
// Public variabler og metoder, som har tilgang til til de private variablene | |
// Disse kalles "priviliged" av Crockford | |
this.name = name; | |
this.fallOffBalcony = function () { | |
numberOfLives--; | |
if ( numberOfLives === 0 ) { | |
die(); | |
} | |
} | |
} | |
// Public metoder som ikke har tilgang til private variabler | |
UnfortunateKitty.prototype.meow = function () { | |
console.log("Meow"); | |
} | |
var pusur = new UnfortunateKitty("Pusur"); | |
pusur.die() // Finnes ikke | |
pusur.fallOffBalcony() // Funker | |
pusur.meow() // Funker |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment