. 1 <! DOCTYPE HTML > 2 < HTML > . 3 < head > . 4 < Meta charset = "UTF-. 8" > . 5 < title > </ title > . 6 < Script type = "text / JavaScript" > . 7 / * . 8 Scope 9 refers to the action of a variable range of 10 a total of two scopes 11 globally scope 12 directly write JS script tag code, the global scopes are 13 global scope created when the page is opened, closed in the page when destroying 14 In the global scope has a global object window, on behalf of the browser window that is created by the browser we can use directly 15 in the global scope, 16 created variables are stored as properties of the window object 17 created methods will save as a method of the window object 18 variables in global scope are global variables, any part of the page can be accessed 19 20 2. the scope of the function 21 to create a function scope, after the function is finished, the role of the function when the function call domain destroyed 22 each call to the function will create a new function scope, they are mutually independent 23 in the function scope can access into the global scope of the global scope can not access to the function scope variables 24 when the scope of the function when operating a variable, it will first look for in their scope, if there is direct use of 25 if not then the next higher level scope looking until you find the global scope, 26 if the global role domain still is not found, an error ReferenceError 27 to access the global variables in the function can be used windo w target 28 29 in the scope of the function is also declared in advance of properties 30 variables declared var keyword, will be declared in the function code is executed before all of the 31 function declarations will be performed in the function code is executed before all of the 32 33 is * / 34 is 35 / * 36 variable declaration in advance 37 with var keyword to declare a variable, it will be declared before any code executes 38 if you do not use the var keyword when declaring a variable, the variable is not declared in advance 39 40 function declaration in advance 41 created using the function declarations in the form of a function function function ( )} { 42 it was created before the execution of all the variables, a statement can be called before 43 function to create a function expression, is not declared in advance, it can not be called before statement 44 is * / 45 var a = 10 ; 46 is the console.log (window.a); 47 Fun (); 48 //fun2 (); 49 // function declarations fun2 advance, can always call the 50 function Fun () { 51 is the console.log ( " Hi Fun " ); 52 is } 53 is // variables declared fun2 advance, but the function is not declared in advance, so variable is not assigned until the following code assignment was only 54 is var fun2 = function () { 55 the console.log ( " Hi fun2 " ); 56 is } 57 is var AA = 123 ; 58 function F () { 59 Alert (AA); // undefined but has been declared assignment 60 var aa=456; 61 } 62 f(); 63 alert(aa);//123 64 </script> 65 <head> 66 <body> 67 </body> 68 </html>