Special attention should be paid to reading and modifying the prototype in javascript, because the reading and writing of the prototype is not equivalent, and the javascript prototype

Source: Internet
Author: User
Tags hasownproperty

Special attention should be paid to reading and modifying the prototype in javascript, because the reading and writing of the prototype is not equivalent, and the javascript prototype

For members inherited from prototype objects, the read and write operations are inherently unequal. For example, assume that the prototype object of object A is B, and the prototype object of object B is null. If we need to read the name attribute value of object A, JS will first search for it in object A. If the name attribute is found, it will be returned. If there is no name attribute in object, search for the name in prototype B. If the name is found, return. If the name is not found in prototype B, the name is still not found because it has reached the top of the prototype chain, returns undefined directly. Execute. name = "aty". If A has the name attribute, the value of the name attribute is modified. If A does not have the name attribute, A new name attribute is added to, there will be no prototype search process.

 

Why do I ignore the prototype when writing? It can also be understood that because the prototype is shared by all objects, this similar copy-on-write mechanism ensures that objects do not affect each other. This mechanism is good or bad, so pay attention to it when using it.

Function Person () {}// add data Person for the Person class prototype. prototype. plainProp = "hi"; Person. prototype. objectProp = {"age": 10}; var a = new Person (); var B = new Person ();. plainProp = "hello"; // re-assign a value, without changing the prototype // No re-assign a value to the reference, just by referencing to modify the attribute a of the object. objectProp. age = 20; alert (. plainProp); // helloalert (B. plainProp); // hialert (. objectProp. age); // 20 alert (B. objectProp. age); // 20

 

The code above shows that the prototype write does have the copy-on-write feature. If you want to know whether the property or method comes from the Object itself or the prototype, you can use the Object. hasOwnProperty () function. Almost all functions in JS do not distinguish whether the data comes from the object itself or its prototype chain. Only this function is differentiated.

// Whether the test property is in its own object or in the prototype alert (. hasOwnProperty ("plainProp") // truealert (. hasOwnProperty ("objectProp"); // false


The laptop cannot be connected to the Internet. "windows cannot be repaired because the process is canceled. If you want to get help, contact the network administrator for a prompt like this,

Reinstall it! WINDOWS is like this.

How can I return the webpage location before submission after using alert in javascript,

Generally there are two-minute methods:

1. Get the focus in the user name input box: document. getElementById ("txtName"). focus ();
2. Set an anchor point and link it to this anchor point: window. location = "# userContent ";

If you have any questions, please HI me ~

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.