In-depth analysis on prototype of ctipt PT: Is prototype attributes a copy, a reference, or a fixed search method? (1): Modify prototype attributes.

Source: Internet
Author: User
In-depth analysis on prototype of ctipt PT: Is prototype attributes a copy, a reference, or a fixed search method? ---- 01 _ modifying prototype attributes//
<SCRIPT type = "text/JavaScript"> var person = function () {}; person. prototype. username = new string ("zhangsan"); var person1 = new person (); var person2 = new person (); alert ("after the instance is created, person1.username:" + person1.username ); alert ("after the instance is created, person2.username:" + person2.username); // modify the property value of the prototype: person. prototype. username = new string ("Lisi"); alert ("after the prototype is modified, person1.username:" + person1.username); alert ("after the prototype is modified, person2.username:" + person2.username ); </SCRIPT>
// Output:

After the instance is created, person1.username: zhangsan
After the instance is created, person2.username: zhangsan

After the prototype is modified, person1.username: Lisi
After the prototype is modified, person2.username: Lisi

 
Q: Why does the attribute values of person1 and person2 of two instances change after the property values of the prototype are modified?
Suppose 1 copy: N instances are created with copies of the prototype attributes. After the prototype attributes are modified, the attribute values of N instances are modified (n times ). in this way, after the prototype property value is changed, the value of the copy on the instance is also modified.
-- It should be impossible to make n modifications!

Suppose 2 references:
N instances are referenced by the prototype property when they are created. At this time, person. prototype. username and person. username references the same object string ("zhangsan"), when the person. prototype. username = new string ("Lisi") after execution, person. prototype. usename references the new object string ("Lisi"), and then modifies the username attribute of N instances so that its username references the same object: string ("Lisi ").
After the prototype property references the new object, the instance property also references the same new object.
-- N modifications are required, which is unlikely.
 

Suppose 3 fixed search methods:
N instances are not created with the same prototype name. For example, when a person is created, it does not have the username attribute! When accessing person. when username is used, because it does not have the username attribute, you can search for it in a fixed way: Because person is an instance of person (person instanceof person gets true), you can find person. if the prototype attribute is not found, continue to the previous prototype search. Here, the JS engine finds the person. prototype. username attribute, so output!
Because of this search method, when the prototype property value person. Prototype. username is modified, no additional operations are required!
Therefore, after the property value of the prototype is changed to "Lisi", the property value of the prototype found by person1.username is also the property value of the prototype!
-- You only need to modify prototype and search by fixed search method. This example can be interpreted!

Return full text

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.