Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the prototype chain work?

Tags:

javascript

var A = {};
var B = Object.create(A);
var C = Object.create(B);
A.isPrototypeOf(C);//returns true
C.isPrototypeOf(A);//returns false

In the above code I'm not understanding the reason behind the result of being false in C.isPrototypeOf(A);

like image 602
Bhojendra Rauniyar Avatar asked Dec 05 '22 04:12

Bhojendra Rauniyar


2 Answers

var A = {}; // A inherits Object
var B = Object.create(A); // B inherits A inherits Object
var C = Object.create(B); // C inherits B inherits A inherits Object

// Does C inherit A?
A.isPrototypeOf(C); // true, yes
// C inherits A because    B inherits A    and    C inherits B

// Does A inherit C?
C.isPrototypeOf(A); // false, no
// A only inherits Object
like image 137
Paul S. Avatar answered Dec 09 '22 14:12

Paul S.


C is "descended" from B which is "descended" from A. How could C be the prototype of A?

As C is ultimately inheriting from A, and A gets nothing from C, then it is false that "C is the prototype of A"

like image 40
JAAulde Avatar answered Dec 09 '22 13:12

JAAulde