JavaScript Scope Understanding

Source: Internet
Author: User

Scope is the accessible scope of variables and functions, that is, scope controls the visibility and life cycle of variables and functions. In JavaScript, the scope of a variable has both global scope and local scope. A variable is not declared or declared within a function without a VAR is a global variable , has a global scope, all properties of the Window object have global scope, can be accessed anywhere in the code, the function is internally declared and var The modified variable is a local variable that can only be used within the function body, although the parameters of the function are not Var but are still local .

Simple JavaScript scopes are well understood, and in some Class C programming languages, each piece of code within the curly braces has its own scope, and the variables are invisible outside the code snippet that declares them, called block-level scopes , This is where JavaScript makes it easy for beginners to misunderstand,JavaScript does not have block-level scopes, only function-level scopes : variables are visible within the body of the function in which they are declared and their child functions. (A larger difference from the C language, allowing nested definition functions).

The JS authoritative guide has a very incisive description: "The functions in JavaScript run in their defined scopes, not in the scopes they are executed in."

To analyze the execution flow of JS code, JS itself is single-threaded, whenever a function is executed, it will produce a new context (execution context), This context is pressed into the context stack of JS, which is ejected after function execution, so the JS interpreter is always executed at the top of the stack. When a new context is generated, the variable object (Variable object, VO)of the context is first bound, including arguments and the variables defined in the function The scope chain that belong to the context is then created, and this is finally given to the object to which this function belongs.

The function is also an object, and it has an internal property for the JS engine to browse, called [[Scope]]. The scope is determined when the function is defined, not when the function is called . Define a function that includes not only the code logic of the function, but also the current scope chain scope Chain

Scope The first object is always the variable object (VO) of the environment where function A is located, and the environment in which function A is in global windows.

Since a is a function, it will continue to press the local variable defined within the function into the top of the Scopechain, where the value of the local variable is not just declared, and is not initialized .

Function call: Generates a new execution context and uses the value in the variable object to initialize the property in the Activation object. The scopechain of the function is then assigned to the [[scope]] property of the execution context. Symbols in the run process are found along the scopechain.

Variables in the body of a function that are stored within the scope of the function definition. This feature is called closures.

 for (var i = 0; i < elements.length;i++) {    (Founction (n) {        = founction () {            Elert (n)        }    }) (i);}   // output:0,1,2,3,..

It should be understood that in the scope of the registered response function, a separate copy of n is stored separately.

PS: Learning about what's and how scope works was the most functions important thing in Javascript.

JavaScript Scope Understanding

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.