简体   繁体   中英

Not able to understand this prototype concept

I was going through an article ` and I came across this code, where it was explaining about prototype, and how it works. What I want is, what does this sentence mean with respect to the code.

This code creates a Date object, then walks up the prototype chain, logging the prototypes. It shows us that the prototype of myDate is a Date.prototype object, and the prototype of that is Object.prototype

const myDate = new Date();
let object = myDate;

do {  
  object = Object.getPrototypeOf(object);
  console.log(object);

} while (object);

It's actually quite straightforward:

const myDate = new Date();   // Create a Date object.
let object = myDate;

do {  
  object = Object.getPrototypeOf(object); // Find the object’s prototype.
  console.log(object);                    // Print to the log.
} while (object);            // Repeat (finding prototypes’ prototypes)
                             // as long as there are prototypes to be found.

So, we will find out that the object's prototype also has a prototype. That's the chain the excerpt talks about.

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