A brief talk about the scope chain in JavaScript

Source: Internet
Author: User

    Javascript中作用域就是变量与函数的可访问范围, that is, the scope controls the visibility and life cycle of variables and functions. The scope of the variable has global scope and local scope two kinds. When looking for a variable, it is looked up from the variable object in the current context, and if it is not found, it is looked up from the variable object in the parent execution context, and the variable object , which is the global context, has been found. the list of variable objects that are composed of multiple execution contexts is called a scope chain .

See Several topics:

A:

1 varA = 12 functionfn1 () {3   functionfn2 () {4 Console.log (a)5   }6   functionFn3 () {7     varA = 48 fn2 ()9   }Ten   varA = 2 One   returnFn3 A } - varfn =fn1 () -FN ()//How much output

observing the code, we find that the result of FN () here is ultimately determined by function fn2 () {console.log (a)}, which executes the fn2, but where there is no declaration a, Therefore, in its upper-level scope, found that there is a local variable var a = 2, so the 4th row Console.log (a), the final output is 2.

B:

1 varA = 12 functionfn1 () {3   functionFn3 () {4     varA = 45 fn2 ()6   }7   varA = 28   returnFn39 }Ten functionfn2 () { One Console.log (a) A } - varfn =fn1 () -FN ()//How much output

observing the code, we found here that the result of FN () is ultimately determined by function fn2 () {Console.log (a)}, which executes fn2, but does not declare a in it, and then looks in its upper-level scope, finds that there is a global variable var a = 1, so the final result output is 1.

C:

1 varA = 12 functionfn1 () {3 4   functionFn3 () {5     functionfn2 () {6 Console.log (a)7     }8     vara9 Ten fn2 () OneA = 4 A   } -   varA = 2 -   returnFn3 the } - varfn =fn1 () -FN ()//How much output

observing the code, we found here that the result of FN () is ultimately determined by FN2 (), in Fn3 () fn2 () at the time of execution, the internal does not declare the variable a, and then the up-level scope to find the local variable A, although it has been declared but has not been assigned (assigned value after), so the final output is Undefined.

Summary:

    1. function in the process of execution, first from their own internal search variables;
    2. If it is not found, then from the scope of the creation of the current function to find, to go up;
    3. Note that the current state of the variable is being searched.

Just write it down here, there's something wrong with the criticism.

Personal blog, quote please specify the source.

A brief talk about the scope chain in JavaScript

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.