When I was reading about prototypal inheritance in MDN, I found this code snippet.
function B(a, b){ }
B.prototype = Object.create(A.prototype, {});
B.prototype.constructor = B;
var b = new B();
For simplicity, I have removed the inner content of the functions. Here B.prototype.constructor
is assigned to B, once it is created. Why this is done and what is role of prototype.constructor in the prototype chain. I found this SO question and one answer is
It's a good practice to reset a constructor after the assignment.
I would like to get a good explanation on this and what is the effect on this in the prototype chain. In MDN Object.prototype.constructor is explained as
Returns a reference to the Object function that created the instance's prototype.
I have tried out following
function A(name) {
this.name = name
}
function B() {
this.getName = function(){
console.log('hello');
}
}
var b = new B();
Here b.constructor
is function A(name)
and also there is another constructor available in b.__proto__.constructor
and both are same. What is the difference between these two. Now when I did the following B.prototype.constructor == B
, b.constructor
is function B()
Now I have created a new object from b
var c = Object.create(b)
So how this is going to affect the prototype chain.
Any help is greatly appreciated. Thanks in advance.