简体   繁体   中英

In javascript "contructor --> prototype" chain is never ending

While debugging the JavaScript prototype chain I came across that when we define a Constructor function, the constructor-->prototype chain is never-ending, it keeps on expanding. I have attached a screenshot and a sample code also for reference.

function Person(first, last, age, gender, interests) {
this.name = {
'first': first,
'last' : last
 };
 this.age = age;
this.gender = gender;
} 

const person1 = new Person("foo", "bar", 24, "Male")

My question is, does JavaScript access it by reference/walking up the chain or is JavaScript actually storing it in memory

The chain is not ever-expanding, it contains a circular reference.

As explained on MDN :

Every constructor function has a prototype property whose value is an object containing a constructor property. This constructor property points to the original constructor function.

The following example demonstrates this:

 function X() {} const x = new X(); // test for strict equality console.log(x.constructor === x.constructor.prototype.constructor); // true

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM