Variable declaration: Using the var keyword declaration, if you use a variable that is not declared, JS automatically declares this variable according to the scope of the variable. If the variable is declared only as an assignment, then the value is undefined. Repeated declaration of variables, in JS will not error, will be based on the last declaration to deal with variables. Variable scope: The scope of a variable is that the program code defines the area of the variable, and global variables can be accessed anywhere within the program code. Included in the {} function, the variable (property) within the object becomes a local variable. Variables defined in the function body become local variables, scope is local, and function parameters are local variables. They only make sense in the function body. In a function body, local variables take precedence over global variables, which means that local variables are preferred when the name of the authority variable is the same as the global variable. Be sure to use the var keyword when defining variables in the body of a function, or JS to look up a global variable with the same name and use it. Functions can be nested, and the variables defined by the parent function can have meaning within the child function. Var space= ' op '; Function par () {Var space = ' par '; Function Son () {Return space;}} Par (); = = Parvar space= ' op '; Function par () {Var space = ' par '; Function Son () {Var space = ' son '; Return space; }}par (); = = Son function variable scope and declaration in advance. A variable declaration is defined in the body of the function and in any function nested within the function. The function scope of JS means that the defined variable is always visible in the body of the function, which means that the variable can be used before it is declared in the function body. Var space = ' global '; function test () {Console.log (space);//=>undefined variable has a definition but no value Var space;space= ' function ';//The variable is given an initial value here, But variables are defined in the function body (var space); Console.log (space); = = function variable to give the initial value} Note: Because of functional scope, the definition of variables in the function body, in the function is always defined, that is, JS in the detection function has a definition of the variable statement when the variable is declared in advance, without assigning a value, The variable is assigned a value when it is executed to an assignment statement. So the upper code equals: Var space = ' global '; Function Test () {Var space; Console.log (space); =>undefined variables are defined but have no value Var space;space= ' function '; The variable is given the initial value here, but the variable is defined in the function body (var space); Console.log (space); = = function variable to give initial value} JS functions unique This feature is called Declaration in advance. Variables as attributes: When life is a global variable, a property of a global object is actually defined, and the secondary attribute is not configurable when passing the VAR keyword, and it cannot be deleted using Delete. When not strictly defined (not practical Var) can be deleted by delete. The JS global variable is a property of the global object, and the local variable is not defined, but it can also be thought of as the property of the function object. JS allows you to refer to global variables using this, but there is no way to refer to local variables. JS scope chain: JS is based on lexical scope: global variables always have meaning in the program, local variables in the function body and function nested function body always have meaning. If you consider a variable as a property of a custom implemented object, you can interpret the scope of the variable in a different angle. Each piece of code has a scope chain associated with it. This scope chain corresponds to a list of objects or linked lists. Then when executing the program, JS will find the scope chain of each object at a time. This variable is defined when this variable is found, and an error is thrown when there is no such variable. Then for the top-level code (without the statement of the function definition), there is a global scope chain. For a function that does not contain nested functions, the scope chain contains two objects: A parameter variable, an object of a local variable, and a global variable object. For nested functions: There are at least three objects: A parameter variable, an object of a local variable, and a global variable object. When defining a function to hold a scope chain, when calling this function, a longer function call is created to add the object to the scope chain, which is more complex for nested functions, redefine the intrinsic function, and create a new object to add to the scope chain. The scope chain is not the same every time the external function is called.
JS variables, scope of variables, scope chain