In-depth analysis on prototype of ctipt PT: Is prototype attributes a copy, a reference, or a fixed search method? (3) add prototype attributes with the same name to the instance

Source: Internet
Author: User
In-depth analysis on prototype of ctipt PT: Is prototype attributes a copy, a reference, or a fixed search method? ---- 03 _ add the Same Name attribute of prototype to the instance

//

<SCRIPT type = "text/JavaScript"> var person = function () {}; person. prototype. username = new string ("zhangsan"); var person = new person (); alert ("person. username: "+ person. username); // Add the person property with the same name as prototype to the instance. username = new string ("Lisi"); alert ("after adding an attribute with the same name as prototype to the instance, person. username: "+ person. username); // Delete the username attribute on the instance and then access the [instance]. will the username be undefined? Delete person. Username; alert ("after deleting the username attribute on the instance, person. Username:" + person. username); </SCRIPT>

//

Output:

After the instance is created, person. Username: zhangsan
 
After adding an attribute with the same name as prototype to the instance, person. Username: Lisi

After deleting the username attribute on the instance, person. Username: zhangsan

Problem: After adding an attribute with the same name as prototype to an instance (Person. Username = new string ("Lisi ")), And then delete this attribute, and then access person. Username again. Why is the returned value of this attribute the same as that of the prototype instead of undefined?

Assume 1 copy:

When the person instance is created, it holds a copy of the prototype property, and then changes the username of the instance to string ("Lisi"). After deleting the property of person. username
At the same time, the copy username = new string ("zhangsan") of the prototype attribute is added to the instance. Therefore, the value of the person. Username accessed again is "zhangsan "..

-- Delete the username attribute of person and add the username attribute of the copy of string ("zhangsan") to person. Obviously, it is impossible!

Suppose 2 references:

When the person instance is created, the reference of the prototype property is held. At this time, both person. Prototype. username and person. Username reference the same object string ("zhangsan"), person. Username
= New string ("Lisi") after execution, person. username refers to the new object string ("Lisi"), when the person. after the username attribute, the person. username re-reference person. prototype. username refers to the object string ("zhangsan ")!

-- After deleting the username of person, it is obviously impossible to add the username attribute that references string ("zhangsan") to person at the same time!

Suppose 3 fixed search methods:

When a person instance is created, there is no username attribute with the same name as the prototype. username = new string ("Lisi") indicates adding the username attribute with the same name as the prototype to the person instance. It references the new object string ("Lisi"). At this time, the person. prototype. username references the object string ("zhangsan"), while person. username references string ("Lisi. when username is used, because person has the username attribute, Lisi is output directly. When Delete
Person. username And then access person. username, because it does not have the username attribute, so follow a fixed search method: Because person is an instance of person (person instanceof person gets true), so 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!

-- When the person. Username attribute exists, it will "Block" the prototype's username attribute. After the person. username is deleted, the prototype's username "Block" is removed, which is reasonable!


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.