Deep understanding of JavaScript (11): Execution context Stack __javascript

Source: Internet
Author: User

Reference: http://www.cnblogs.com/wangfupeng1988/p/3989357.html

Continue with the above content.

When the global code is executed, an execution context environment is generated, and each invocation of the function produces an execution context. When the function call completes, the context and the data in it are eliminated and then returned to the global context environment. There is only one execution context environment in the active state.

In fact, this is a stack of stacks of the process-execution context stack. The following figure:

According to the following code to detail the context stack of the stack, the stack process.

As on the code.

The global context environment is first created before the code is executed.

Then the code executes. Before the code executes to line 12th, variables in the context environment are assigned during execution.

Executes to line 13th, calling the bar function.

Jumps inside the bar function, and before executing the function body statement, a new execution context environment is created.

and the execution context of this implementation of the stack, set to active state.

Executes to line 5th, and the FN function is called. Into the FN function, the execution context of the FN function is created and the stack is set to the active state before the function body statement is executed.

Wait for line 5th to finish, that is, after the FN function completes, this call FN generated the context of the stack, and was destroyed (has been used, it is necessary to destroy in time, free memory).

Similarly, after the 13th row completes, that is, the bar function completes, the call bar function generated by the context of the stack, and was destroyed (has been used, it is necessary to destroy in time, free memory).

Well, I am very patient to introduce a short code of the execution context of the change process, a complete closed loop. The variable assignment procedure for the context environment I omitted a lot, because those are not difficult, a look at the know.

Speaking of this, I have to regret to say to you: in fact, we have demonstrated that the above is a relatively ideal situation. There is a situation, and it is often used in a situation, can not be done so cleanly said destruction destroyed. This situation is great-closures.

To say closure, we have to start with the free variable and scope.

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.