Skip to content

Instantly share code, notes, and snippets.

@haeric
Last active December 27, 2015 01:59
Show Gist options
  • Save haeric/7248568 to your computer and use it in GitHub Desktop.
Save haeric/7248568 to your computer and use it in GitHub Desktop.
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