Execution of JavaScript

Source: Internet
Author: User

Tag: The label that appears. NET Ext principle reference function definition

The following content is referenced: http://blog.csdn.net/cxiaokai/article/details/7552653

Http://www.jb51.net/article/36755.htm

First, for an HTML document, the browser is progressively parsing the page structure and information from top to bottom.  JavaScript code should also be part of an HTML document as an embedded script, so the order in which JavaScript code is loaded is determined by the order in which the script tags <script> appear.     And, whether in the HTML script, or through the <script src= "" > Such a reference to the outside of the JS file script, is in its original order to load. Do not postpone execution because it is an external JavaScript file!!

JS an important part is the function, the function is defined in roughly two ways:

function Fn1 () {alert ("Hello world!"    //varfunction() {alert ("Hello wild!"

And for a JS script, its execution is in fact divided into two steps, first "precompiled", and then "implementation period."

In the pre-compilation process, the function of the definition will be executed first, and all VAR variables will be created, the default value is undefined, to improve the execution efficiency of the program.

During page loading, the browser scans every JS block (or file) on the page or load, and if a defined function is encountered, it is preprocessed (similar to a compilation of c, etc.), and the processing is completed before starting from top to bottom, and when an assignment function is encountered, it simply assigns the function to a variable without preprocessing ( Similar to the 1 variables must first be defined after the principle of reference), to be called when the processing. (Note: The assignment of a variable is done during the interpretation execution phase, and if a variable is used before that, its value will be undefined)

function Fn1  () {alert ("Hello world!"   

Normal execution, pop-up "Hello world!", the browser to Fn1 preprocessing, and then from Fn1 (); Start execution.

var function () {alert ("Hello wild!"   

Firebug Error: FN2 is not a function, the browser does not preprocess the Fn2, sequential execution, so the error Fn2 undefined.

This is because the function defined in the example above is only assigned as a value to the variable Fn2, so during the precompiled period, the JavaScript interpreter can only handle declaring the variable F, whereas for the value of the variable F, only wait for the execution period to be assigned in order, there will naturally be a syntax error indicating that object F cannot be found.

3. Processing of code blocks and JS files
"code block" refers to a pair of <script type= "text/web Effects" ></script> tags wrapped js code, file refers to the file, nonsense: D
The browser scans each block or file independently, and then executes the global code sequentially (as mentioned in 2). Therefore, in a block (file), a function can be defined after the call, but in two blocks, the block in which the function is defined must precede the block to which the function is called.
Very around the mouth, see examples of good:
The code is as follows:

<script type= "Text/javascript" >function  Fn () {alert ("Hello world!"

Error: Fn is notdefined, two blocks in exchange.

Although JavaScript is executed by block, different blocks belong to the same global scope, meaning that variables and functions between blocks can be shared.


4. Repeating the definition function overrides the previous definition
This is the same as the repeating definition of the variable, the code:
The code is as follows:

function fn () {alert (1function  fn () {Alert (2

Eject: "2"


If this is the case:
The code is as follows:

function fn () {alert (1function  fn () {Alert (2

or pop Up: "2"

or pop Up "2", why? 2 said all right ...  

Execution of JavaScript

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.