Understanding the free variables in JS and the advanced scope of scopes

Source: Internet
Author: User

If you don't know what the scope is, it is recommended that you first look at what the scope chain is and what the prototype chain is. This article because the content is related.

What is a free variable?

If I define a variable A in the global, and then I use this a in the function, the A can be called a free variable, so to understand that any variable that spans its own scope is called a free variable.

var a = "Chasing the dream Son"; function B () {    // dream }b ();

The variable A in the above code is a free variable, because when function B executes to Console.log (a), it finds that the variable A is not found in the function, then it goes to the previous layer, and finally finds the global variable A.

Scope's Advanced

I said when I was talking about the scope chain. If there is a global variable A, and there is a variable a in the function, then only the variable a in the function is a, there is a situation is more complicated, we look at this code together.

var aa =; function A () {    console.log (AA);} function B (FN) {    var aa = one;     //  A

The final print is not 11 but 22, why is this? Before I explain, I suggest you take a look at the execution context in JS, the Novice Beginner Foundation. This article, if you do not understand the execution sequence of JS is more difficult to understand, and if you look at me these days of the article should have a feeling that the relevance of the content is particularly strong, which is why many JS novice friends can not understand the place.

We went on to say why the print is 22, together to analyze this piece of code.

If our code is like this,

var aa =; function A () {    console.log (AA);}

Print out is 22, I think we should have no opinion, but one thing I must mention, that is, when creating this function, the scope of the function has been decided, but not when the call , this sentence to the tube important.

Does it make sense to understand that the code above is not worth mentioning?

In order to care for the novice friend, I will analyze the process.

First we create a global variable AA

var aa = 22;

Then we created a function A.

function A () {    console.log (AA);}

At this time, JS parsing this function, it has decided the scope of the function A, both if the function a can not find the variable AA that will be found in the global variable, if found to return this AA, if not found on the error.

And then we created a function B.

function B (FN) {    var aa = one;    fn ();}

In function B we define and redefine the variable AA, Although we redefine the variable AA at this time, but since the scope of function A is determined at the time of creation, the variable AA created in function B and the variable AA in function A are not related.

function B (FN) {    var aa = one;    fn ();} b (a);

We passed function A to function B, and as the parameter of function B, then we executed the passed function A, and the last print was 22.

When this function is created, the scope of the function is determined, but not when it is called.

Understanding the free variables in JS and the advanced scope of scopes

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.