JavaScript has "pre-parsed" behavior through the JavaScript authoritative guide and online information. It is important to understand this feature, or you may encounter a lot of unresolved problems in actual development, or even cause bugs in the program. In order to resolve this phenomenon, but also as a study of their own summary, this article gradually guide you to understand the JavaScript "pre-analysis", if my opinion is wrong, but also to correct.
(1) If JavaScript is only parsed at runtime from the top down, it is understandable that the following code will work correctly, because we define the function before calling it.
Copy CodeThe code is as follows:
function ShowMsg ()
{
Alert (' This is message ');
}
ShowMsg (); This is message
(2) We also know that the function can be defined after the calling code, the following code is also working. It seems that showmsg () is not defined when calling ShowMsg (), but working correctly indicates that JavaScript is "pre-parsed".
Copy CodeThe code is as follows:
ShowMsg (); This is message
function ShowMsg ()
{
Alert (' This is message ');
}
(3) above is an example of a function, the following is an example of a common variable. The following example run will pop up undefined, indicating that the first sentence of the MSG is already defined, just not initialized, and it is associated with var msg; Alert (msg); it's the same. If you comment out the second sentence below, the "msg undefined" error is reported. This also indicates that JavaScript is "pre-parsed".
Copy CodeThe code is as follows:
Alert (msg); Undefined
var msg= ' This is message ';
(4) Look again at an example to deepen the "pre-parsing" impression of JavaScript. The following code you will see two times the pop-up dialog box is displayed for this is message 2, why is it this way? In fact, the following one defines two functions of the same name, followed by ShowMsg () overwrite the previously defined (in JavaScript, the same name variable will have a overwrite problem), is equal to the first showmsg () scrapped. Why does the second invocation of ShowMsg () not call the message 1 function defined above it? This proves once again that JavaScript has "pre-parsed" behavior.
Copy CodeThe code is as follows:
ShowMsg (); This is message 2
function ShowMsg ()
{
Alert (' This is Message 1 ');
}
ShowMsg (); This is message 2
function ShowMsg ()
{
Alert (' This is Message 2 ');
}
(5) JavaScript "Pre-parsing" is the pre-parsing of variables or functions into the environment in which they can be called (Variable Runtime 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 and cannot be called directly outside, which indicates that JavaScript "pre-parsing" Not all defined variables are parsed uniformly into a global object, such as window.
Copy CodeThe code is as follows:
function ShowMsg ()
{
var msg= ' This is message ';
}
Alert (msg); MSG Not defined
(6) JavaScript "Pre-parsing" is segmented, accurately said to be divided into <script> blocks. The following code appears in two script blocks on the same page, and defines three functions with the same name. The result of the program operation indicates that the second script block's showmsg () does not overwrite the first two showmsg (), while the second showmsg () of the first script block overrides the ShowMsg ().
Copy CodeThe code is as follows:
<body>
<script type= "Text/javascript" >
ShowMsg (); This is message 2
function ShowMsg ()
{
Alert (' This is Message 1 ');
}
function ShowMsg ()
{
Alert (' This is Message 2 ');
}
</script>
<script type= "Text/javascript" >
function ShowMsg ()
{
Alert (' This is Message 3 ');
}
</script>
</body>
Webflash
Source:http://webflash.cnblogs.com
JavaScript Pre-parsing