1

If I have an inheritance method set up like this:

function A() {}


function B() {
    A.call(this,{});
}

B.prototype = Object.create(A);
B.prototype.constructor = B;

function C() {
    B.call(this, {});
}

C.prototype = Object.create(B);
C.prototype.constructor = C;


var a = new A();
var b = new B();
var c = new C();

Is it possible to confirm, that the instance c has A in it's prototype chain, or has the information been lost somewhere?

LongInt
  • 1,739
  • 2
  • 16
  • 29

1 Answers1

1

No !!!. In your example

C.prototype = Object.create(C);
C.prototype.constructor = C;

You have a prototype of C instances set to C

If you change to C.prototype = Object.create(B);

you will see this

enter image description here

But this doesn't mean that your c is instanceof A. Why ? Because you have set the prototype to an Function not to an Object

If you will change your code to this, that its prototype will be an object

function A() {}


function B() {
    A.call(this,{});
}

B.prototype = Object.create(new A());
B.prototype.constructor = B;

function C() {
    B.call(this, {});
}

C.prototype = Object.create(new B());
C.prototype.constructor = C;


var a = new A();
var b = new B();
var c = new C();

In this case your c has A in it's prototype chain.

You can test it with c instanceof B and c instanceof A in the console

enter image description here

Suren Srapyan
  • 66,568
  • 14
  • 114
  • 112