Day 112th: Function pre-parsing and execution phase in JavaScript

Source: Internet
Author: User

About the functions in JavaScript :
1, Pre-analysis: All the function definition in advance, all the variable declaration ahead of time, the assignment of the variable is not in advance
2, execution: From top to bottom execution, but with exception (callback function in Settimeout,setinterval,ajax, function in event need to trigger execution)

The argument to a function can be a function that can be called directly

function can be a return value
Nesting of functions to form closures
function has two identities:
1. Objects
2. Constructor function

First, the definition

Pre-parsing : In the current scope, JS run before, will have the var and function keyword in advance declaration, and in memory arrangements. Then execute the JS statement from top to bottom.

Pre-parsing occurs only on variables and function defined by var .

1. var

When a variable defined by the var keyword is pre-parsed: It is declared declare, regardless of whether it is assigned, it will be assigned a value of undefined.

alert (a); Undefined
var a = 1;
alert (b); Undefined
var B = function () {
}
alert (c); Undefined
var C;
As long as it is defined by var , whether it is a variable, or a function, is the first assignment undefined, if it is a variable , and regardless of whether the variable is assigned, in the pre-parsing phase , will be assigned to the value of undefined.

2. Function

When a function is pre-parsed, it is not only declared but also defined (define), but the space in which it stores data is stored in the code as a string, without any meaning.

alert (a); The popup is the following function
function A () {
Alert ("Pre-analytic function")
}

3. Attention to this situation

It is not feasible to define a function to be executed immediately , as in the pre-interpretation, it breaks it down into two parts, the first part is the FN function, and the second part is (), an anonymous function , Error when executing. If the parentheses with parameters, such as (2), although not error, will print out 2, but not the FN execution, and can not be passed as a parameter to the FN function.

function fn() {
Code Area
}()
If you want to implement a function that executes immediately, you can put the function you want to execute into a pair of parentheses, for JavaScript , parentheses () cannot contain statements, so at this point, the parser parses the function keyword, the corresponding code is parsed into a function expression, rather than a function declaration, so long as the curly braces enclose the code (including part of the function and a pair of curly braces in the back). As follows:

(function fn () {
Code area ...
}())
Can also be written as: closures.

(function () {
Code area ...
})();

4, pre-analysis need to pay attention to the situation    Pre-parsingis happening inCurrent ScopeAt the beginning, we pre-parse the Global ScopeIn JSIn our GlobalIs ours. window

when we run the function, we generate a new private scope (each execution is new, Destroy when execution is complete) we can understand this scope as opening up a new memory space. In this memory we also execute pre-parse . When our function is done, the memory or scope will be destroyed
if a variable under the current scope is not pre-parsed, it will be looked up to its upper level until window is found, if window The does not have a definition, it will be an error. Therefore, the variable defined by var within a function is local variable , No can over var defines a variable that is global variable .
Pre-parsing does not recur on the same variable, that is, if a variable has been pre-parsed under the current scope, no more parsing will be repeated. The function to the right of the
equals sign does not pre-parse.
alert (a);
FN ();
var a = function fn () {};
First print undefined, the second error, undefined, because pre-parsing, the right side of the = number is not pre-parsed.

The pre-interpretation is not affected by other if or other judging conditions, that is, if the condition is not established, we will have a var or function in it as a pre-explanation .
alert (a); Undefined
if (1==2) {
var a=12;
}
Defined later overrides the previously defined
alert (a); Eject function on the back
function A () {
var b;
}
alert (a); Still pops up the back function, because function is pre-parsed in advance
function A () {
var C;
}
JavaScript "Pre-parsing" is segmented, accurate is divided into <script> blocks.

When one of our functions returns a new function, we define a variable outside to receive it, so the memory of the function cannot be destroyed automatically after execution, which is what our so-called function memory is taking up.

The value of a variable depends on where it is defined, this, to see where it is called.

Day 112th: Function pre-parsing and execution phase in JavaScript

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.