Understanding of javascript scopes and scopes
The JavaScript scope is the accessible range of variables and functions. 1. The variable scope is in JavaScript, and the variable scope is divided into global scope and local scope. Variables with global scopes can be defined anywhere. variables declared without var (excluding function parameters) have a global scope and become global variables. Therefore, you must declare local variables using var 2. all properties of window have a global scope. variables declared by the outermost function in vitro also have the global scope var globalScope = "globalScope"; function checkScope () {var partScope = "part"; scope = "scope ";} checkScope (); console. log (globalScope); // globalScope, global variable console. log (scope); // scope, global variable console. log (partScope); // Uncaught ReferenceError: partScope is not defined, error: partScope not defined Local Scope 1. variables declared with var in the function body have local scopes and become local variables. 2. function parameters also have a local scope var globalScope = "globalScope"; function checkScope (x) {scope = "scope"; // changed to the global variable var partScope = "part "; // In the function body, you must add var globalScope = "partScope";} checkScope (12); console to declare a local variable. log (globalScope); // globalScope, In the function body, the priority of local variables is higher than that of the global variable console. log (scope); // scope, global variable console. log (partScope); console. log (partScope); // Uncaught ReferenceError: partScope Is not defined. The console cannot be accessed outside the local variable. log (x); // summary cannot be accessed outside local variables: the priority of local variables in the function body is higher than that of global variables with the same name. To declare local variables, you must use var 2. function scopes some class C languages are block-level scopes. Each curly braces is a scope, and the code in the curly braces is invisible to the outside. JavaScript is a function scope without block-level scope. No matter where the variables in the function are declared, they are visible to the entire function. That is, all the variables declared in the JavaScript function are pushed to the top of the function body, but they are declared in advance, the value assignment of variables is retained in the original position. Function scopes can only be used to declare independent scopes. Not every curly braces are independent scopes. For example: the for loop cannot create a local scope for (var I = 0; I <5; I ++) {var scope = 'scope ';} console. log (I) // 5; I still exists, because JavaScript is a function scope rather than a block-level scope console. log (scope); // scope; scope still has function checkScope () {console. log (funScope); // undefined, all the variables declared in the function are advanced to the top of the function body, so funScope will think that there is already var funScope = "funScope ";} CheckScope (); 3. scope chain: JavaScript variables are all attributes of objects, and this object may be attributes of other objects, and all objects are attributes of global objects, therefore, the relationship between these objects can be seen as a chain. The chain header is the object where the variable is located, and the end of the chain is the Global Object function checkScope (scope) {var funScope = "funScope "; var s = scope;} when looking for the value of the variable funScope, it first looks for the current object. If the current object cannot be found, it continues to look for the next object above the scope chain, if no object in the scope chain contains this attribute, a reference error is thrown. In a function that does not contain nesting, the scope chain has two objects: 1. define the object of function parameters and local variables 2. global objects are contained in nested functions, with at least three objects in the scope chain.