0

Why we need set Child.prototype.constructor property to Child after inheritance. For example

function Parent(a){
    this.a = a;
}

function Child(a, b){
    Parent.call(this. a);
    this.b = b;
}
Child.prototype = Object.create(Parent.prototype);
Child.prototype.constructor = Child // 1)**

As I see the instance of constructor will be the same without // 1) ** line

Why we need this // 1) ** line?

Nikolay Podolnyy
  • 931
  • 10
  • 19
  • 2
    Possible duplicate of [Why is it necessary to set the prototype constructor?](https://stackoverflow.com/questions/8453887/why-is-it-necessary-to-set-the-prototype-constructor) – James Thorpe Sep 08 '17 at 11:32

1 Answers1

0

Because it should not be the same

By the language syntax Child.prototype.constructor should be the instance of Object.create(Parent.prototype).constructor which is actually Parent.prototype.constructor

It then up to implementation or optimization of the engine. But it cannot trusted so that // 1) line was to ensure it

Thaina Yu
  • 1,372
  • 2
  • 16
  • 27