Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save wesleybliss/b3c3fc2cf9c662aa3378 to your computer and use it in GitHub Desktop.
Save wesleybliss/b3c3fc2cf9c662aa3378 to your computer and use it in GitHub Desktop.
JavaScript Classical Inheritance #2
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