JS note-scope (Execution context [execution context], activity object) Closure

Source: Internet
Author: User

(1) scope:

First, every function in Javascript is an object and an instance of the funtion object. funtion has a series of internal attributes that are only accessible to the JavaScript engine, one of which is [[Scope]. it contains a set of objects in the scope created by a function, which is the scope chain of 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, define the following function:

Function add (num1, num2 ){
VaR sum = num1 + num2;

Return sum;

}

In the Add functionCreateIts scope chain is filled with a single variable object, that is, a global object, which contains all global variables, as shown in (note: the image only lists part of all the variables ):

The scope of function add will be used during execution, assuming var Total = add (5, 10 );RunThis function createsExecution ContextAn execution context defines the environment in which a function is executed. The execution context corresponding to each function execution is unique, therefore, multiple calls to a unified function may result in the creation of multiple execution contexts. After the function is executed, the execution context is destroyed.Each execution context has its own scope chain for identifier parsing, When the execution context is created, its scope chain is initialized to the objects contained in the [[Scope] of the currently running function.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 called"Activity object(Activation object )"As a variable object in the function runtime, the activity object contains all the local variables, named parameters, parameter sets, and this of the function, that isIt is initialized through the arguments attribute of the function,The value of the arguments attribute is an arguments object:

The arguments object is an attribute of an activity object. It includes the following attributes:

    1. callee-reference to the current function
    2. length-Number of actually passed parameters
    3. properties-indexes (an integer of the string type) attribute value is the parameter value of the function (arranged from left to right by the parameter list ). The number of elements in properties-indexes is equal to that in arguments. length. the value of properties-indexes is shared .

In fact, this sharing is not really a shared memory address, but two different memory addresses. the JavaScript engine is used to ensure that the two values are the same at any time. Of course, this also has a premise, that is, the index value is smaller than the number of parameters you pass in. That is to say, if you pass in only two parameters and continue to use the arguments [2] value assignment, it will be inconsistent, for example:

 
FunctionB (X, Y, a) {arguments [2] = 10; alert (a);} B (1, 2 );

At this time, because the third parameter A is not passed, the result of alert (a) is still undefined after the value of 10, instead of 10, but as follows:CodeThe pop-up result is still 10, because it has nothing to do with.

 
FunctionB (X, Y, a) {arguments [2] = 10; alert (arguments [2]);} B (1, 2 );

] Then this object will be pushed to the front end of the scope chain. When the runtime context is destroyed, the activity object will also be destroyed. Shows the new scope chain:


During function execution, every time a variable is encountered, an identifier parsing process is performed to determine where to obtain and store data. This process searches for identifiers with the same name from the scope chain header, that is, from the activity object. If the identifiers are found, the variables corresponding to the identifiers are used, if the next object in the scope chain is not found, and if no object is found after the search, the identifier is considered undefined. In the process of executing a function, each identifier must undergo such a search process. NOTE: If two variables with the same name are stored in different parts of the scope chain, the identifier is the one that is first found during the traversal of the scope chain.

(2): Closure

Closure. Simply put, nested functions in a function are as follows:

Function (){

VaR name, age;

Function B (){

Console. Log (name );

Console. Log (AGE );

}}

In this example, function B is a closure.The closure is powerful in that it allows the function to access data outside the scope. For example, if function B accesses the variable in function a, let's look at an example:

Funtion assignevents (){

VaR id = "xdi9592 ";

Document. getelementbyid ("Save-BTN"). onclick = function (event ){

Savedocument (ID );

}}

The event processor in the assignevents function is a closure.Assignevents is created during execution and can access the ID variable of the scope.WhenWhen assignevents () is executed, an active object containing the variable ID and other data is created, that is, an object in the context scope of the runtime. When the closure is created, its [[Scope] attribute is initialized to these objects,


Because the [[Scope] attribute of the closure contains references of the same objects as the runtime context scope chain, when the execution of the outer function is complete, because the reference still exists in the [[Scope] attribute of the closure, the active object cannot be destroyed, resulting in high memory consumption, that is, memory leakage.

When a closure is executed, an execution context is created again. The two identical scope chain objects referenced in the scope chain and attribute [[Scope] are initialized at the same time, then an activity object is created by the closure itself,

Http://blog.csdn.net/wangxiaohu__/article/details/7260668

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.