Understanding about prototype disconnection in JavaScript

Source: Internet
Author: User

I recently saw a post on prototype chain disconnection in the Forum. The original Article is as follows:

Var sound = {echo: function () {alert ("sound") ;}}; function Person () {this. name = "name _" ;}; Person. prototype = sound; // ---- bind it earlier ---- var fo1o = new Person (); var xxxxxx = Person. prototype; // ---- bind later ----?? Xxxxxx. yyyy = "yyyy _"; alert (fo1o. echo); // alert (undefined); alert (fo1o. yyyy); // alert (undefined ); //////////////////////////////////////// //////////////////////////////////////// /// // var sound = {echo: function () {alert ("sound") ;}}; function Person () {this. name = "name _" ;}; var fo1o = new Person (); Person. prototype = sound; // ---- bind it later ---- var xxxxxx = Person. prototype; // ---- bind later ----?? Xxxxxx. yyyy = "yyyy _"; alert (fo1o. echo); // alert (function (){....}); alert (fo1o. yyyy); // alert ("yyyy _"); // Why alert (fo1o. yyyy) will be affected.

The alert comment here is reversed, but this is not the point. Why is the second alert comment undefined? Later, I conducted an experiment to test the following content:

1. alert (fo1o. name); // name _

2. Person. prototype. name = '_ name'; alert (fo1o. name); // name _

3. Person. prototype. name = '_ name'; var fo2o = new Person (); alert (fo2o. name); // name _

The above three experiments are mainly to test whether prototype can modify the original attributes of an object. The result is no longer possible, because in fact, in addition to the inherent (that is, when declared) attributes and methods of each object, there is also a prototype object, and prototype is mainly used to expand the functions of the original object. That is to say, if you access an object's attributes, you must first find them in the inherent attributes. If you cannot find them, go to the properties in the prototype object. For details, see the following two figures:


The first graph declares a class Person, which has an inherent attribute name and a prototype attribute age, and then declares the object a of a Person, then its name attribute is located from the Person class? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> encrypt/ytcTE2rTmu7nKx9K70fmjrNXiwO +/ydLUsNFwcm90b3R5cGW/tNf20ru49ta41euho8v50tTL + encrypt + decrypt/decrypt Example/O5ss/txNq05sHLo6zL/MPHtcTBqs + 1vs274cfQts + example/D5tf2tcTI/example + example/example = "hello, world", then how to pass Pe What if the rson object accesses the name attribute of prototype?

This is the end of my understanding. If any of the above statements is incorrect, please let me know. Thank you.

Related Article

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.