Last active
December 17, 2015 01:39
-
-
Save jeremyckahn/5530482 to your computer and use it in GitHub Desktop.
Simple example of inheritance in JavaScript.
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
| 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