Created
June 29, 2013 00:36
-
-
Save rsolomo/5889137 to your computer and use it in GitHub Desktop.
random javascript inheritance example
This file contains 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
// 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