• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 193
  • Last Modified:

JavaScript Implement Inheritance

I am trying to implement inheritance but it does not seem to work.  I am following the example in "Programming in HTML5 with JavaScript  and CSS3".  I created a vehicle constructor and then created a car that is supposed to inherit from vehicle but I'm getting an error.  See below in jsfidle.


Any ideas?
2 Solutions
IsaacSharePoint Client Side DeveloperAuthor Commented:
When I run jshint, it says "Car" is already defined but in the book, they have it executing with no errors.
Alexandre SimõesManager / Technology SpecialistCommented:
I don't understand what you're trying to achieve.
You're creating a Vehicle base "class" that has 3 properties and 2 methods, and this works well.

After you create the Car but I don't understand what you want to do with it.
Also, inside Car you're setting its prototype by calling Car.prototype, but car is not defined yet... you're still inside the functions, you haven't returned anything.

If all you want is to add the wheelQuantity property you can do:

Here's the code:
var Vehicle = (function () {
    // create the vehicle
    function Vehicle() {
        this.year = null;
        this.make = null;
        this.model = null;
    // isolate vehicle methods in its prototype
    Vehicle.prototype = {
        getInfo: function () {
            return this.year + ' ' + this.make + ' ' + this.model;
        startEngine: function () {
            return 'Vroom';
    return Vehicle;

// create the car that has Vehicle as its prototype
//    and adds wheelQuantity as new property
var Car = function (year, make, model) {
    this.year = year;
    this.make = make;
    this.model = model;
    this.wheelQuantity = 4;
Car.prototype = new Vehicle();

// now to simulate an implementation, 
//    lets create a Fiat Punto car instance
var fiatPunto = new Car(2012, 'Fiat', 'Punto');

// Test the result

Open in new window

RobOwner (Aidellio)Commented:
I would do it slightly differently, utilising the Object.create() function:


function Vehicle(year, make, model) {
  this.year = year;
  this.make = make;
  this.model = model;
Vehicle.prototype.getInfo = function() {
  return this.year + ' '+ this.make + ' '+this.model;   
Vehicle.prototype.startEngine = function() {
  return 'Vroom';   

var v2 = new Vehicle(2015,'Jeep', 'Wrangler');

function Car(year, make, model) {
  Vehicle.call(this, year, make, model);
  this.wheelQuantity = 4;

Car.prototype = Object.create(Vehicle.prototype);

Car.prototype.getInfo = function() {
  return 'Vehicle Type: Car ' + Vehicle.prototype.getInfo.call(this);

var v3 = new Car(2015,'Jeep', 'Wrangler');

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now