2

Traditionally the object inheritance looks like the following:

function Parent() {
   console.log('parent constructor');
}
Parent.prototype.method = function() {
   console.log('parent method');
}


function Child() {
   this.parent.apply(this,arguments);
   console.log('child constructor');
}

Child.prototype = Object.create(Parent.prototype);
Child.prototype.constructor = Child;
Child.prototype.parent = Parent;

but this is works too and looks better in my opinion:

function Parent() {
   console.log('parent constructor');
}
Parent.prototype.method = function() {
   console.log('parent method');
}


function Child() {
   this.constructor.apply(this,arguments);
   console.log('child constructor');
}

Child.prototype = Object.create(Parent.prototype);
// without next lines, constructor will be Parent
// Child.prototype.constructor = Child; 
// Child.prototype.parent = Parent;

So,

What is the real reason to use first method ?

What problems I can get if I will use second method ?

zb'
  • 8,071
  • 4
  • 41
  • 68

1 Answers1

1

What is the real reason to use first method ?

In fact neither of them does properly work. If have another class that inherits from Child, this.parent and this.constructor will not point to the expected function - and you'll either skip a level or get into infinite recursion.

What problems I can get if I will use second method ?

(new Child).constructor would be Parent, which is kinda unexpected. See Is there a good use case for the constructor property in Javascript?. And above problem of course still exists, regardless whether you use parent or constructor for the property name to refer to your parent class.

For a proper "super" call reference the parent constructor explicitly:

function Child() {
   Parent.apply(this,arguments);
   console.log('child constructor');
}
Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • > In fact neither of them does properly work. http://jsfiddle.net/x9puo0rd/ , http://jsfiddle.net/x9puo0rd/1/ – zb' Mar 11 '15 at 12:06
  • seems you still not give any practical reason, excluding "unexpected" , for me it is clearly expected, because spec says so. – zb' Mar 11 '15 at 12:09
  • @eicto: Please read carefully, I said "*if another class inherits from `Child`*": http://jsfiddle.net/x9puo0rd/2/ – Bergi Mar 11 '15 at 12:09
  • yes, I see the problem , i remember I solved it before, checking how, – zb' Mar 11 '15 at 12:09
  • sorry, main question still can be rephrased as "why one can have neccessarity to write `Child.prototype.constructor = Child` ? I have example http://jsfiddle.net/oceog/902z097y/ (check end of the `inherit()` function ) – zb' Mar 11 '15 at 17:34
  • probably yes, btw I just found one case when it may be usefull, if you need to allow your methods to make instance of itself (`var myInstance=new (Object.getPrototypeOf(this).constructor)()`) – zb' Mar 11 '15 at 17:39
  • 1
    Yes, `new this.constructor()` is one use case. No `getPrototypeOf` required, though :-) – Bergi Mar 11 '15 at 17:43