JavaScript-js tutorial

Source: Internet
Author: User
A previously unknown concept-closure emerged in JavaScript. What is a closure? On the surface, it is a closed package, which is related to the scope. So, let's talk about the scope before the closure.
GLOBAL SCOPE
Local Scope
Scope chain
Execution Context
Activity object
Closure
Closure Optimization

A previously unknown concept-closure emerged in JavaScript. What is a closure? On the surface, it is a closed package, which is related to the scope. Therefore, we should talk about the scope before talking about the closure.

Scope)

Generally, variables and functions used in a piece of program code are not always available. The scope of availability is the scope. The use of the scope improves the locality of the Program Logic and enhances the reliability of the program, reduce name conflicts.

Global Scope)

Objects that can be accessed anywhere in the Code have a global scope. The following situations have a global scope:

1. the outermost function and variables defined outside the outermost function have a global scope. For example:

The Code is as follows:


Var outSide = "var outside ";
Function outFunction (){
Var name = "var inside ";
Function inSideFunction (){
Alert (name );
}
InSideFunction ();
}
Alert (outSide); // correct
Alert (name); // Error
OutFunction (); // correct
InSideFunction () // Error


2. undefined variables that directly assign values are automatically declared to have a global scope. For example:

The Code is as follows:


BlogName = "CSDN Li Da"


3. All properties of the window object have a global scope. For example, the built-in properties of the window object have a global scope, such as window. name, window. location, and window. top.

Local Scope)

The Code is as follows:


Function outFunction (){
Var name = "inside name ";
Function inFunction (){
Alert (name );
}
InFunction ();
}
Alert (name); // Error
InFunction (); // Error


Scope chain)

In JavaScript, everything in JavaScript is an object, including a function. Function objects, like other objects, have attributes that can be accessed by code and a series of internal attributes that are only accessible by the JavaScript engine. An internal attribute is the scope, which contains the set of objects in the scope created by the function. It is called the scope chain of the function and determines which data can be accessed by the function.
After a function is created, its scope chain will be filled with accessible data objects in the scope of the function. For example:

The Code is as follows:


Function add (num1, num2 ){
Var sum = num1 + num2;
Return sum;
}


When the function add is created, its scope chain is filled with a global object, which contains all global variables, as shown in (Note: The image only lists part of all variables):


It can be seen that the function scope chain is created when the function is created.

Execute context)

The scope of function add will be used during execution, for example:

The Code is as follows:


Var total = add (5, 10 );


When the add function is executed, JavaScript creates an Execute context, which contains all the information required during the runtime of the add function. Execute context also has its own Scope chain. When a function is running, the JavaScript engine first initializes the Scope chain of the execution context by using the Scope chain of the add function.

Active Object)

The JavaScript engine then creates an Active Object. These values are copied to the scope chain of the runtime context in the order they appear in the function, they form a new object, activation object, which contains all the local variables, parameters, and such variables during the function runtime, this object will be pushed to the frontend of the scope chain. When the runtime context is destroyed, the activity object will also be destroyed. Shows the new scope chain:

Execution context is a dynamic concept. When a function is created during operation, the Active Object of the activity Object is also a dynamic concept. It is referenced by the scope chain of the execution context and can be concluded: the execution context and the activity object are both dynamic, and the execution context's scope chain is initialized by the function scope chain.
During function execution, each variable is used to retrieve and store data from where it is obtained. This process searches for identifiers with the same name from the scope chain header, that is, from the activity object, if the variable corresponding to the identifier is found, the next object in the scope chain will be searched. If no object is found, the identifier is considered undefined, during function execution, each identifier must undergo such a search process.
Closure (closure)
Let's take a look at an example of javascript code:

The Code is as follows:


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.