In-depth understanding of JavaScript prototypes and Closures (14)-from "Free variables" to "Scope chains"

Source: Internet
Author: User

Let's first explain what "free variables" are.

The variable x used in a scope is not declared in a scope (that is, declared in other scopes), and for a scope, X is a free variable. Such as

As in the previous program, when the FN () function is called, the 6th line in the function body. The value of B can be taken directly in the FN scope, because B is defined here. When you take the value of x, you need to take it to another scope. To which scope to fetch it?

Some people say that to go to the parent scope, in fact, sometimes this interpretation will produce ambiguity . For example:

So, don't use the above statement. In contrast, it would be more appropriate to use this sentence description--to take a value in the scope where the function was created--"create" rather than "call," remember --this is what is called a "static scope".

For the first paragraph of this article, in the FN function, when taking the value of the free variable x, which scope to take? --Go to the scope where the FN function was created--no matter where the FN function will be called.

What is described above is just a cross-step scope to look for.

What if it's a step that hasn't been found yet? --Then cross! --continues across the global scope. If it's not found in the global scope, it's really gone.

This step-by-step "cross" route, as we call it, is a chain of scopes .

Let's summarize this "scope chain" procedure taken from a variable: (assuming A is free)

In the first step, now the current scope looks for a, if any, gets and ends. if not, continue;

The second step, if the current scope is a global scope, proves that a is undefined, ends; otherwise continues;

The third step, (not the global scope, which is the function scope), will create the scope of the function as the current scope;

Fourth step, jump to the first step.

In the above code: line 13th, FN () returns the bar function, assigned to X. Executes X (), which executes the bar function code. When the value of B is taken, it is removed directly from the FN scope. When you take the value of a, you try to take it in the FN scope, but you cannot get it, but you can only move to the scope where you created the FN to find it.

This section seems to be easy to bring out the scope chain, and finished. It's all easy to have the foundation of the previous sections, otherwise it will be difficult to explain.

Let's start with a formal talk about a friend who's always looking forward to it--closures. Please look forward to the next section.

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

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 (14)-from "Free variables" to "Scope chains"

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.