The scope of JS is functional division, not block, so JS scope is divided into global scope and local scope (function scope).
All variables that belong to the global scope are properties of the Window object
JS variable has a very important concept, scope chain
Look at an example to analyze
var str1 = ' global ';
function T1 () {
Console.log (STR1); <span style= "color: #222222;" >global</span>
Console.log (STR2); <span style= "color: #ff0000;" >uncaught REFERENCEERROR:STR2 is not defined</span>
str2 = ' local ';
}
T1 ();
Then analyze a
var str1 = ' global ';
function T1 () {
Console.log (STR1); Global
Console.log (STR2); Undefined
var str2 = ' local ';
}
T1 ();
Analysis: JS code from top to bottom, but JS code in the overall operation is divided into: Lexical analysis period and running period, that is, before the Top-down implementation, first has a "lexical analysis process." From the above results for example, the first analysis of the T1 function, analysis of the T1 var str2 local variables, but at the end of the function execution, because the STR2 value is undefined, in short, is to declare in advance but not assignment, (only for the value of VAR declaration) so the above code is equivalent to
var str1 = ' global ';
Function t1 () {
var str2 = ';
console.log (STR1);//global< br> Console.log (STR2); Undefined
str2 = ' local ';
}
T1 ();