Skip to content

Instantly share code, notes, and snippets.

@rsolomo
Created June 29, 2013 00:36
Show Gist options
  • Save rsolomo/5889137 to your computer and use it in GitHub Desktop.
Save rsolomo/5889137 to your computer and use it in GitHub Desktop.
random javascript inheritance example
// Random inheritance example
function Vehicle(color) {
this.color = color
}
Vehicle.prototype.move = function() {
// Do some stuff here
}
function Car(color, drivetrain) {
// Calling the Vehicle constructor
Vehicle.call(this, color)
this.drivetrain = drivetrain
}
// Inherit the prototype, do not use new here.
// Call super constructors in the constructor.
// Optionally, you can add the constructor as an non-enumerable property here,
// but you may have a hard time shim'ing this in older browsers.
Car.prototype = Object.create(Vehicle.prototype)
var car = new Car('red', '4wd')
console.log(car instanceof Car) // true
console.log(car instanceof Vehicle) // true
console.log(car.drivetrain) // '4wd'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment