In-depth understanding of javascript prototype and closure (18)-addition: Relationship between context and scope, and in-depth understanding of javascript

Source: Internet
Author: User

In-depth understanding of javascript prototype and closure (18)-addition: Relationship between context and scope, and in-depth understanding of javascript

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

 

Let's look at the differences between the two in simple languages.

00 context:

It can be understood as an invisible object (with several attributes). Although invisible, it does exist because all variables are stored in it, otherwise, where should we store the Defined variables?

In addition, the context environment is created when a function is called, which is easy to understand. Using parameters as an example, if you do not call a function, where do I know what parameters you want to pass to me?

01 scope:

First, it is abstract. Second, remember one sentence: In addition to global scopes, only functions can create scopes. Create a function and create a scope. No matter whether you call a function or not, if you create a function, it has an independent scope and has its own "territory ".

02 both:

A single scope may contain several context environments. There may have never been a Context Environment (a function has never been called); there may have been. Now, after a function is called, the context environment is destroyed; one or more closures may exist at the same time ).

 

It doesn't matter if the above text is not understood, and let's look at the example below.

First, in addition to the global scope, each function must create a scope. Variables between scopes are independent of each other. Therefore, x in the global scope and x in the fn scope have no relationship with each other and do not affect each other.

 

Second, a global context environment is generated before the program is executed, and the variables are assigned a value during the program execution.

 

Third, when the program executes Row 3 and calls fn (5), the context of fn (5) is generated, the stack is pressed, and the status is set to active.

 

Fourth, after executing the 17th rows, the fn (5) return value is assigned to f1. At this time, the execution of the Context Environment returns to the global, but the context environment of fn (5) cannot be destroyed, because there is a reference to the closure (you can refer to the previous article and will not go into details here ).

 

Fifth, continue to execute row 18th and call fn function -- fn (10) again ). Generate the fn (5) Context Environment, press the stack, and set it to the active state. However, at this time, the Context Environment of fn (5) is still in the memory-two context environments exist under one scope at the same time.

Here, the focus has been discussed, and the subsequent scenarios will not be described here.

The purpose is to clarify the relationship between the context and scope through this example. Of course, it doesn't have to be like an academic term to describe the concept. A simple understanding is helpful for the use of closures.

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

This article has been updated to the directory "deep understanding of javascript prototype and closure series". For more information, see "deep understanding of javascript prototype and closure series".

In addition, you are welcome to follow my microblog.

You are also welcome to follow my other tutorials-don't worry, they are all free!

Microsoft petshop4.0 source code explanation video json2.js source code interpretation video

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


I want to learn more about the javascript operating mechanism, such as closures, prototype chains, and scopes. What books do I recommend?

The above two authoritative guides and advanced programming can be viewed.

I recommend the essence of the javascript language written by Zhou aimin

Javascript closure and prototype

Since your B is used as a parameter of object a, it must be used as an attribute that needs to be stored and used for multiple times. If only a few methods call B, just let B be the parameter of the method ~
<Script type = "text/javascript">
Function (){}

A. prototype. say = function (B ){
Alert (B );
}
Var x = new ();
X. say ("xx ");
</Script>

If it is your incoming B parameter, it will be used in many places. It seems quite cost-effective to add a type attribute ~~

Function a (B ){
This. type = B;
}

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.