Skip to content

Instantly share code, notes, and snippets.

@jeremyckahn
Last active December 17, 2015 01:39
Show Gist options
  • Select an option

  • Save jeremyckahn/5530482 to your computer and use it in GitHub Desktop.

Select an option

Save jeremyckahn/5530482 to your computer and use it in GitHub Desktop.
Simple example of inheritance in JavaScript.
function Car (licensePlate) {
this.licensePlate = licensePlate;
}
Car.prototype.isLegal = function () {
return !!this.licensePlate;
};
function Mustang (licensePlate) {
Car.call(this, licensePlate);
}
Mustang.prototype = new Car();
Mustang.prototype.make = 'Ford';
var mustang = new Mustang('bullitt');
console.log(mustang.make); // 'Ford'
console.log(mustang.isLegal()); // true
var car = new Car();
console.log(car.make); // undefined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment