Created
August 28, 2014 16:27
-
-
Save wesleybliss/32b23ac1c340315800d0 to your computer and use it in GitHub Desktop.
JavaScript Classical Inheritance #1
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( 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