Scope and scope chain

Source: Internet
Author: User
Tags try catch

First, Scope1.1. Global scope(1) The outermost function and the variables outside the outermost function have global scope:
1 varI=1;2 functionA () {3 varaw= "QW";4 Console.log (AW);5 functionBa () {6 varQq= "AAA";7 }8 }9 Ten Console.log (i); One Condole.log (AW); A a (); -  - //1 the //uncaght Referenceerror:aw is not defined (...) - //QW

(2) All non-declared variables are automatically declared to have global scope:
1 functionC () {2o= "QW";3  4    functionBa () {5P= "AAA";6    }7 BA ();8    }9 c ();Ten   One Console.log (o); A Console.log (p); -  - //QW the //AAA

o when the function C () finishes executing, the global scope is available, and the function BA () is declared as a global variable after the function is executed(3) In general, all properties under Window have global scope by default, such as: Window.onload,window.scroll, etc. second, local scopeIn contrast to global scopes, local scopes only function within certain code snippets and are only available internally
1 functionew () {2 vare=2;3 functionQ () {4 varS=2,w=3;5 Console.log (s);6 vard=s+W;7 }8 q ();9 Console.log (d);Ten } One undefined A ew (); - //2 - //uncaught referenceerror:d is not defined (...)

second, the scope chainin JavaScript, functions are objects, and in fact, everything in JavaScript is an object. function objects, like other objects, have properties that can be accessed through code and a series of internal properties that are accessible only by the JavaScript engine. One of the internal properties is [[Scope]], defined by the ECMA-262 Standard third edition, which contains a collection of objects in the scope of the function being created, called the scope chain of the function, which determines which data can be accessed by the function.
function fo () {var a=3; }
here, before the function text is called, the scope chain

Fo ()

When this function is executed, an internal object called the runtime context is created, and the runtime context defines the environment at which the function executes. Each run-time context has its own scope chain, which is used for identifier resolution, when the runtime context is created, and its scope chain is initialized to the object contained by [[Scope]] of the currently running function. These values are copied into the scope chain of the run-time context, in the order in which they appear in the function. Together they form a new object called an "active object", which contains all the local variables, named arguments, parameter sets, and this of the function, which is then pushed into the front of the scope chain, and the active object is destroyed when the run-time context is destroyed. The new scope chain is as follows:

third, code optimizationIt is known from the scope chain that the global variable is usually at the deepest of the scope chain and the lookup time is longer, so we want to avoid calling the global table, while the WITH statement and the catch in the try catch change the scope chain, avoid using with as much as possible, because the try Catch Statements are useful for debugging exceptions, so I can optimize them.  

 

Scope and scope chain

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.