Created
July 16, 2013 12:31
-
-
Save gecbla/6008294 to your computer and use it in GitHub Desktop.
JavaScript and OO Inheritance
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
// 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'; | |
} |
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
// 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