Skip to content

Instantly share code, notes, and snippets.

@gecbla
Created July 16, 2013 12:31
Show Gist options
  • Save gecbla/6008294 to your computer and use it in GitHub Desktop.
Save gecbla/6008294 to your computer and use it in GitHub Desktop.
JavaScript and OO Inheritance
// Child class
function Dog() {
// Call parent constructor
Pet.call(this);
// Override parents name-property
this.name = 'Ceasar';
}
// Is child of pet
Dog.prototype = new Pet();
// Uses its own constructor
Dog.prototype.constructor = Dog;
// Override public method: whatPet
Dog.prototype.whatPet = function() {
return 'Im a flying superdog';
}
// Parent class
function Pet() {
// Privileged property
this.name = 'Nyan';
// Privileged function
this.getName = function() {
return this.name;
}
// Privileged function
this.setName = function(_name) {
this.name = _name;
}
}
// Public method
Pet.prototype.whatPet = function() {
return 'Im just a generic pet!';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment