0

I have the inheritance chain Vehicle -> Motorized -> Car implemented:

function Vehicle()
{
    var m_name;

    this.setName = function(pName) {
        m_name = pName;
    };

    this.getName = function() {
        return m_name;
    };
}

function Motorized()
{
    var m_started = false;

    this.start = function() {
        m_started = true;
        console.log(getName() + " started");
    };
}

function Car()
{ }

//set up the inheritance chain
Motorized.prototype = new Vehicle();
Car.prototype = new Motorized();

// use
var lCar = new Car;
lCar.setName("Focus");
console.log(lCar.getName()); // Focus
lCar.start(); // ReferenceError: getName is not defined

When I invoke lCar.start() (defined in function Motorized), I get an ReferenceError: getName is not defined. How can I use the inherted method getName() in my subclass Motorized?

Simon
  • 3,043
  • 1
  • 35
  • 66

1 Answers1

1

Because Javascript doesn't know where to look for your getName() method. You can clarify the syntax declaring a self variable that always points to the right object, like this:

function Vehicle()
{
    var self = this; // Vehicle
    var m_name;

    this.setName = function(pName) {
        self.m_name = pName;
    };

    this.getName = function() {
        return self.m_name;
    };
}

function Motorized()
{
    var self = this; // Motorized
    var m_started = false;

    this.start = function() {
        /*
         `self` is Motorized, with proto Vehicle, so
         has a getName() method.
         `this` instead is the object where you call
         start() from, i.e. Car, in the example down below.
         */
        self.m_started = true;
        console.log(self.getName() + " started");
    };
}

function Car()
{ }

//set up the inheritance chain
Motorized.prototype = new Vehicle();
Car.prototype = new Motorized();

// use
var lCar = new Car;
lCar.setName("Focus");
console.log(lCar.getName()); // Focus
lCar.start(); // Focus started

Note that in this case, using the keyword this instead of self throughout the code would have worked as well, but you definitely cannot omit it before getName(). Also, if you are planning to add more code later on, such as event handlers in jQuery, having a clear reference to the class you're coding in can be useful, as this can become easily ambiguous, at least from the human point of view.

Anyway, whether using self is a bad coding pattern or not is the topic of this question; the point in your example is that you need to call self.getName() or this.getName().

Community
  • 1
  • 1
Pietro Saccardi
  • 2,482
  • 32
  • 37
  • I did not consider to use the function invocation pattern here. Thank you. – Simon Jul 06 '14 at 09:48
  • Yes also the `()` were missing but it would have raised exception as well. The method `getName()` is present in `Motorized` and `Car` but the keyword `this` could be tricky, [see this](http://stackoverflow.com/questions/3127429/javascript-this-keyword), so this can help with debugging issues. – Pietro Saccardi Jul 06 '14 at 09:54