Created
February 5, 2015 05:37
-
-
Save wesleybliss/b3c3fc2cf9c662aa3378 to your computer and use it in GitHub Desktop.
JavaScript Classical Inheritance #2
This file contains hidden or 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
var Person = function() { | |
this.helloMessage = 'Hello'; | |
}; | |
Person.prototype.sayHello = function() { | |
console.log( this.helloMessage ); | |
}; | |
var Nihonjin = function() { | |
this.helloMessage = 'Konnichiwa'; | |
}; | |
// Inherit the parent class's prototype | |
Nihonjin.prototype = new Person(); | |
// Reset the constructor back to the child class | |
Nihonjin.prototype.constructor = Nihonjin; | |
Nihonjin.prototype.bow = function() { | |
console.log( '*bows*' ); | |
}; | |
// Could also override Nihonjin.sayHello here if you want | |
var person = new Person(); | |
var nihonjin = new Nihonjin(); | |
person.sayHello(); | |
nihonjin.sayHello(); | |
nihonjin.bow(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment