3

I have two classes Car and Maruti. When i use inheritance by assigning the prototype of Maruti to new Car object. Why do i have to set the Maruti.prototype.constructor to Maruti. Shown in the code below.

function Car(){
}

function Maruti(){
}

Maruti.prototype = new Car()
Maruti.prototype.constructor = Maruti

Unable to understand the last line of the code. Can someone explain in detail?

Henrik Andersson
  • 45,354
  • 16
  • 98
  • 92
Devang Paliwal
  • 291
  • 2
  • 4
  • 3
    Ad the title: are you asking us whether you are unable to understand .prototype.constructor? Problably yes, otherwise you wouldn't be asking. – Tomas Jul 18 '13 at 06:33
  • Constructor can be used in the following way: `Person.prototype.haveChild=function(){return new this.constructor()}` That should return a new Person but if you have Person inherit from some other object you just overwritten the Person.prototpe.constructor so it points to the wrong constructor. You may never use constructor and a lot of example code will just ignore it (like the ever posted `Maruti.prototype = Object.create(Car.prototype)`) But if you (or someone extending or using your code) want to use it later then you need to fix it. – HMR Jul 18 '13 at 07:43
  • It's better not to call the "Parent" constructor code when you set up inheritance. http://stackoverflow.com/questions/16063394/prototypical-inheritance-writing-up/16063711#16063711 under `Setting prototype without calling the constructor` – HMR Jul 18 '13 at 07:45

3 Answers3

0

If Maruti inherit Car using Maruti.prototype = new Car(), you need to reset the constructor property for the class Maruti using Maruti.prototype.constructor=Maruti;, otherwise instances of Maruti would have a constructor of Car.

No Idea For Name
  • 11,411
  • 10
  • 42
  • 70
0

Before you set Maruti.prototype = new Car the prototype of Maruti has only one property, constructor, which is set to Maruti itself:

function Car() {}

function Maruti(){}

console.log(Maruti.prototype); // logs the following
                               // {
                               //     constructor: Maruti
                               // }

Maruti.prototype = new Car;
Maruti.prototype.constructor = Maruti;

After you set the prototype of Maruti to new Car the constructor property is lost (because its not the same object). Instead Maruti.prototype.constructor will now be Car (because its inherited from Car.prototype). Hence we set the constructor property to Maruti again.

For a more visual explanation take a look at the following answer: https://stackoverflow.com/a/8096017/783743

Community
  • 1
  • 1
Aadit M Shah
  • 72,912
  • 30
  • 168
  • 299
0

Given this,

function Car(){
}

and that the constructor property is a property of the functions prototype

Now, if you look up the constructor of a new Car instance

new Car().constructor === Car

You see it is Car, then you are setting

function Maruti(){
}
Maruti.prototype = new Car();

Remember that new Car().constructor === Car?,
as such is now, Maruti.prototype.constructor === Car, hence shadowed down to the Maruti instance, overriding the original Maruti.prototype.constructor

So, if you create a new Instance of Maruti and lookup the constructor

new Maruti().constructor === Car

You see it is Car, though new Maruti() is constructed by Maruti and not by Car

Moritz Roessler
  • 8,542
  • 26
  • 51