Skip to content

Instantly share code, notes, and snippets.

@csanz
Created August 14, 2012 20:39
Show Gist options
  • Save csanz/3352746 to your computer and use it in GitHub Desktop.
Save csanz/3352746 to your computer and use it in GitHub Desktop.
Super Simple JavaScript & Inheritance Example
// 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