Created
August 14, 2012 20:39
-
-
Save csanz/3352746 to your computer and use it in GitHub Desktop.
Super Simple JavaScript & Inheritance Example
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
// Generic Object | |
function Spaceship(){} | |
Spaceship.prototype.navigate = function(distance, direction){ | |
console.log(['moved',distance,'miles towards',direction].join(' ')) | |
return this | |
} | |
// Specific Spaceships | |
function BWing(){ | |
this.range = 200 | |
} | |
BWing.prototype = new Spaceship() | |
BWing.prototype.shoot = function(direction){ | |
console.log(['shot missile', this.range,'miles', direction].join(' ')) | |
} | |
// Create new BWing ship! | |
var bwing1 = new BWing() | |
bwing1.navigate(400, 'south') | |
bwing1.shoot('north') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment