1 <!DOCTYPE HTML>2 <HTML>3 <Head>4 <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8">5 <title>Untitled Document</title>6 <Script>7 //Scope8 vara=0;9 Ten functionABC () { One vara=1; A alert (a); - functionC () { - alert (a); the } - C () - } - + ABC () - //alert (a) + //alert (a); A </Script> at </Head> - - <Body> - </Body> - </HTML>
Results: Two times a=1 was popped;
Explanation: First executes the function ABC (), A=1 is the local variable inside, takes precedence over the global variable, so the first time alert result is ABC's 1;
Then execute the function C (), Functions C is a sub-function of ABC, and function C does not have a variable, so it will go to his parent, that is, the variable var a=1 abc, then the C pop-up is also 1.
function inside the function is called the closure of the function: the child function can call the parent function variable, if the child function cannot find the variable, then the entire scope chain of variables will be saved