In-depth understanding of JavaScript prototypes and Closures (18)-Supplement: Context and scope relationships

Source: Internet
Author: User

This series has a lot of space to explain the context and scope, and some people reflect that these two are the same thing. This article uses a small example to illustrate that the scope and context are definitely not the same thing.

Before we start, let's summarize the differences in simple language.

00 Contextual Environment:

Can be understood as an invisible object (there are several attributes), although invisible, but it really exists, because all the variables are stored inside, otherwise we define where the variable is saved?

In addition, for a function, the context is created at the time of invocation, which is well understood. Take the argument as an example, you don't call the function, where do I know what parameters you want to pass me?

01 Scope:

First, it's very abstract. Second, remember the word: Except for global scopes, only functions can create scopes. Create a function to create a scope, whether you call not called, the function as long as it is created, it has a separate scope, there is a "site" of its own.

22 Persons:

A scope may contain several context environments. It is possible that there has never been a context (the function has never been called); it is possible that, now that the function has been called, the context is destroyed; it is possible that one or more (closures) exist at the same time.

The above text does not understand OK, and see the following example.

First, in addition to the global scope, each function creates a scope. The variables between scopes are independent of each other. Therefore, x in the global scope and in the scope of the FN, the two do not matter, do not affect each other, peaceful coexistence.

Second, the global context is generated before the program executes, and the variables are assigned values when the program executes.

Third, the program executes to line 17th, calling FN (5), which produces the FN (5) context, and presses the stack, and sets it to active state.

Finally, after the execution of Line 17th, the return value of FN (5) is assigned to F1. At this point the execution context is back to the global, but the context of FN (5) cannot be destroyed because there is a closure reference (which can be viewed in the previous article, which is not covered here).

Five, proceed to line 18th and call the FN function--fn (10) again. Generates the FN (5) context and presses the stack and sets it to the active state. At this point, however, the context of FN (5) is still in memory-there are two contexts in a single scope.

Here, the emphasis has been told, and then the scene here will not repeat.

The goal is to hope that we can use this example to clarify the context and scope of the relationship. Of course, it doesn't have to be like a college pie. The concept of a word, a simple understanding of the use of closures is helpful.

---------------------------------------------------------------------------

This article has been updated to the directory of in-depth understanding of JavaScript prototypes and closures, and more on the in-depth understanding of JavaScript prototypes and closures series.

Also, please pay attention to my Weibo.

Also welcome to follow my other tutorials-don't be afraid, are free!

"Microsoft petshop4.0 Source Interpretation Video" "Json2.js Source Interpretation video"

--------------------------------------------------------------------------

In-depth understanding of JavaScript prototypes and Closures (18)-Supplement: Context and scope relationships

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.