Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wesleybliss/32b23ac1c340315800d0 to your computer and use it in GitHub Desktop.
Save wesleybliss/32b23ac1c340315800d0 to your computer and use it in GitHub Desktop.
JavaScript Classical Inheritance #1
var Person = function( name ) {
this.name = name;
};
Person.prototype.hello = function() {
console.log( this.name + ' says hello!' );
};
Person.prototype.walk = function( distance ) {
console.log( this.name + ' walked ' + distance + ' steps' );
};
Person.prototype.run = function( mph, distance ) {
console.log( this.name + ' ran ' + distance + ' steps at ' + mph + ' mph' );
};
var Olympian = function( name ) {
Person.call( this, name );
};
Olympian.prototype = Object.create( Person.prototype );
Olympian.prototype.constructor = Olympian;
Olympian.prototype.jump = function( height ) {
console.log( this.name + ' jumped ' + height + ' feet' );
};
Olympian.prototype.run = function( mph, distance ) {
Person.prototype.run.call( this, (mph * 3), (distance * 3) );
};
var person = new Person( 'Alpha' );
person.hello();
person.walk( 20 );
person.run( 5, 100 );
var olympian = new Olympian( 'Beta' );
olympian.hello();
olympian.walk( 20 );
olympian.run( 10, 100 );
olympian.jump( 50 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment