Understanding JavaScript Pre-resolution _javascript techniques

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.
Copy Code code as follows:

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. When it looks like the call to ShowMsg () is showmsg () or undefined, but it works, it indicates that JavaScript is "resolvable."
Copy Code code as follows:

ShowMsg (); This are message
function ShowMsg ()
{
Alert (' This are 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."
Copy Code code as follows:

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 are showing this are message 2, why does this happen? In fact, the following previous definition of two functions with the same name, followed by the ShowMsg () to cover the previous definition (in JavaScript, the same name variable will have an overlay problem), equal to the first showmsg () scrap. Why does the ShowMsg () of the second call not invoke the message 1 function defined above it? This again proves that JavaScript has a "pre resolution" behavior.
Copy Code code as follows:

ShowMsg (); This is message 2
function ShowMsg ()
{
Alert (' This was Message 1 ');
}
ShowMsg (); This is message 2
function ShowMsg ()
{
Alert (' This was 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.
Copy Code code as follows:

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

(6) JavaScript "Pre-Resolution" is staged, accurately said to be divided into <script> block. The following code appears in two script blocks of the same page, and three functions with the same name are defined. The results of the program operation indicate that the showmsg () of the second script block does not overwrite the preceding two showmsg (), while the second showmsg () of the first script block overwrites the first showmsg ().
Copy Code code as follows:

<body>
<script type= "Text/javascript" >
ShowMsg (); This is message 2
function ShowMsg ()
{
Alert (' This was Message 1 ');
}
function ShowMsg ()
{
Alert (' This was Message 2 ');
}
</script>
<script type= "Text/javascript" >
function ShowMsg ()
{
Alert (' This was Message 3 ');
}
</script>
</body>

Author: Webflash
Source: http://webflash.cnblogs.com
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.