Solved

JavaScript Implement Inheritance

Posted on 2015-02-14
3
182 Views
Last Modified: 2015-02-15
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.

http://jsfiddle.net/isogunro/nj9x5zbn/1/

Any ideas?
0
Comment
Question by:Isaac
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 5

Author Comment

by:Isaac
ID: 40610247
When I run jshint, it says "Car" is already defined but in the book, they have it executing with no errors.
0
 
LVL 30

Assisted Solution

by:Alexandre Simões
Alexandre Simões earned 150 total points
ID: 40610751
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:
http://jsfiddle.net/AlexCode/r49am6rf/1/

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
console.log(fiatPunto.make);
console.log(fiatPunto.wheelQuantity);
console.log(fiatPunto.getInfo());

Open in new window

0
 
LVL 43

Accepted Solution

by:
Rob earned 350 total points
ID: 40610763
I would do it slightly differently, utilising the Object.create() function:

http://jsbin.com/zenexe/2/edit?js,console

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');
console.log(v2.getInfo());
console.log(v2.startEngine());

//Implement INHERITANCE
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');
console.log(v3.getInfo());
console.log(v3.startEngine());

Open in new window

0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question