How to Use js to modify prototype attributes

Source: Internet
Author: User
Tags hasownproperty

Prototype defines the attributes and methods that can be accessed by all instances of a specific type. In many cases, you need to assign a new value to the attributes in the prototype. The following describes how to modify the prototype.

In javascript, prototype defines the attributes and methods that can be accessed by all instances of a specific type. In many cases, you need to assign a new value to the attributes in the prototype, if the method is incorrect, it may lead to unexpected situations (just fighting for a newbie like me). Below we will give a simple summary of this knowledge through testing. The basic type is defined as follows: Code: function Person () {} Person. prototype = {constructor: Person, name: "person", age: 100, friends: ["a", "B"], getName: function () {return this. name ;};define two Person instances and modify the name attribute in the instance (this attribute is defined in prototype). The test code is as follows: var p1 = new Person (); var p2 = new Person (); document. write (p1.name + "<br/>"); // person document. write (p2.name + "<br/>"); // person p1.name = "p1"; document. write (p1.name + "<br/>"); // p1 doc Ument. write (p2.name + "<br/>"); // person document. write (p1.hasOwnProperty ("name") + "<br/>"); // true belongs to the object document. write (p2.hasOwnProperty ("name") + "<br/>"); // false belongs to the prototype document. write (Object. keys (p1) + "<br/>"); // name document. write (Object. keys (p2) + "<br/>"); // empty document. write (Object. getOwnPropertyNames (Person. prototype) + "<br/>"); // constructor, name, age, friends, getName document. write (Person. prototype. Name + "<br/> "); // after testing, we can find that p1.name = "p1" does not modify the name value, but adds a new name attribute in instance p1 to overwrite the name attribute in prototype, from the subsequent judgment, we can see that the name attribute of p1 is already an instance attribute, not a prototype attribute, and the Object following it. keys (p1) can also be seen that the instance p1 has a name attribute while p2 does not. All passing in js is a value transfer. This value can be a pointer to the reference type, so the equal sign does not mean to modify the reference object, but instead switches the original reference relationship, the following code illustrates this problem: var obj = new Object (); obj. name = "obj"; function changeObj (o) {o. name = "changed"; o = new Object (); o. name = "newObj";} changeObj (obj); document. write (obj. name); // changed in the changedObj method, o = new Object () does not modify the parameter o value, but disconnects the original reference relationship, therefore, the result is not newObj, but changed. Next, test and modify the friends attribute in prototype. The reference type code is as follows: p1.friends. push ("c"); document. write (p1.friends + "<br/>"); // a, B, c document. write (p2.friends + "<br/>"); // a, B, c p1.friends = ["x", "y", "z"]; document. write (p1.friends + "<br/>"); // x, y, z document. write (p2.friends + "<br/>"); // a, B, c document. write (p1.hasOwnProperty ("friends") + "<br/>"); // true belongs to the object document. write (p2.hasOwnProperty ("friends") + "<br/>"); // false belongs to the prototype document. write (Object. keys (p1) + "<br/>"); // name, friend document. write (Object. keys (p2) + "<br/>"); // empty document. write (Object. getOwnPropertyNames (Person. prototype) + "<br/>"); // constructor, name, age, friends, getName document. write (Person. prototype. friends + "<br/>"); // The test results of a, B, and c are basically the same as those of the first test. When the equal sign is changed, this will cut off the original reference and create a new attribute for the instance and overwrite the Same Name attribute in prototype. Based on the two test results, we found that the value type attribute in prototype cannot be directly modified in the instance (of course this value type should not be defined in prototype, the code example here only describes this knowledge point and has no practical significance)

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.