JS prototype object, each new object has a separate prototype object __proto__

Source: Internet
Author: User

When I read a blog post, I tested the prototype chain of JavaScript, the prototype object, and found that each constructor (assigned to a prototype ) New object has its own independent prototype object __proto__.

Prototype and __proto__ point to the same object, one is "class" above, and the other is "pair like" above. Prototype literally "type" means that this represents a "class", a category, a collection, not a specific one.

Similar to the relationship between classes and objects in the C + + language. And __proto__ is based on the specific class "prototype", instantiated to deal with a specific object, through the prototype new different objects __proto__ are independent of each other.

It seems wrong!! See

Wrong, wrong, the original test code below the understanding of the wrong. When we assigned S.name, we did not change to the Name property of the S.__proto__ object. Instead, the Name property is added to the S object. When its output is s.name,

Did not go to s.__proto__ above the name attribute, but directly on the S object itself found on the Name property , so do not upward in the s.__proto__ prototype chain to find the name attribute.! I just got it wrong, but it's a good reaction.

The test code is as follows:

function Fun () {  //constructor}; Fun.prototype = {name: ' YSR '};var s  = new Fun (), s.name = ' pxk '; Console.log (s.name);//pxkvar t = new Fun (); CONSOLE.L OG (t.name); Ysr

  

JS prototype object, each new object has a separate prototype object __proto__

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.