The principle and implementation of JavaScript pre-parsing

Source: Internet
Author: User

In fact, or some kind of phenomenon prove that is not the case, through the "JavaScript Authority Guide" and the relevant information on the Internet to learn that JavaScript has "pre-resolution" behavior. It is important to understand this feature, otherwise you may encounter a lot of unresolved problems in actual development, and even cause bugs in the program. In order to resolve this phenomenon, also as one of their own learning summary, this step-by-step guide you to understand the JavaScript "pre-resolution", if my opinion is wrong, I also hope to correct.

(1) If JavaScript is only the Run-time parsing from the runtime, it is understandable that the following code works correctly because we first define the function before calling it.

function showmsg {alert (' This are message ');} showmsg; This are message

(2) We also know that functions can be defined after calling code, and the following code works as well. It looks like the showmsg is still undefined when calling ShowMsg, but it works, which means that JavaScript is "resolvable."

ShowMsg; This are message function showmsg {alert (' The ' is Message ');}

(3) The above is an example of a function, and here is an example of a common variable. The following example runs will pop up undefined, indicating that MSG is already defined in the first sentence, just not initialized, it is with Var msg; Alert (msg); it's the same. If you comment out the second sentence below, you will report a "msg undefined" error. This also indicates that JavaScript is "pre resolved."

Alert (msg); Undefined var msg= ' this are message ';

(4) Take another example to deepen the impression of JavaScript "pre-resolution". The following code you will see two pop-up dialog boxes that show this are message 2, why do they do this? In fact, the following one defines two functions with the same name, followed by showmsg covering the previous definition (in JavaScript, the same variable with the same name would have an overlay problem), Equal to the first showmsg scrapped. Why does the second call ShowMsg not invoke the message 1 function defined above? This again proves that JavaScript has a "pre resolution" behavior.

ShowMsg; This are message 2 function showmsg {alert (' the ' is Message 1 ');} showmsg; This are message 2 function showmsg {alert (' the ' is Message 2 ');}

(5) JavaScript "Pre-resolution" is to resolve variables or functions to the environment in which they can be invoked (variable run-time environment). The following code looks like the definition of MSG before alert (msg), but the program runs or reports "msg undefined" error, because the variable defined in the function is a private variable of the function, which cannot be called directly outside, indicating that JavaScript "pre-resolution" Not all defined variables are uniformly parsed into a global object, such as window.

function showmsg {var msg= ' this are message ';} alert (msg); MSG Not defined

(6) JavaScript "Pre-resolution" is segmented, accurate to say is the

The above is the principle and implementation of JavaScript pre-parsing, I hope to help you.

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.