Shallow JavaScript said in scope

  Today met a question, talk about your understanding of the scope of JavaScript

  There are two scopes 

  1. Global scope

    As the name suggests, the variable anywhere in the code can be used to access the global scope.

    E.g

    • Undefined variable will direct assignment

      

(function() {
  var a = b = 5;
})(); console.log(a);
// a is not defined console.log(b); // 5

 

    • All the properties window object, e.g. window.name, window.location board and the like.

  2. Local Scope

    Instead the local scope and global scope, some of the variables in the code segment to have access to local scope. Generally there such a variable in the function, the local scope is sometimes called function scope.

    

  Sometimes global scope and local scope confusing

var a =. 1 ; 
test (a); 
function test (a) { 
    a = 2 ; 
  the console.log (a); // 2 } the console.log (a); //. 1

// test because the inside of a global and a variable is not the same, test modification of the local variable a, and console.log printing is a global variable a, so print out or 1
var a =. 1 ; 
test (a); 
function test () { 
    a = 2 ; 
} 
the console.log (a);     // 2 

// found less here a test parameter function, a function test can be performed so that a direct assignment for the variable is not defined, as a global variable. When two global variables of the same name appears, the value will cover the front of the global variable after the operation of the off
// console.log so printed is a function of executing the test later obtained a = 2;
var a = 1;
test(a);
function test(b){
    b = 2;
}
console.log(a);      // 1;
console.log(b);      // b is not defined;

 


The scope chain is a critical presence

Introducing the concept of ~~~~~~~~~~~~~~~~~ drop ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ · ~ ~ ~

"JavaScript Advanced Programming" 4.2 Scope and execution environment

  Execution Environment (execution context) JavaScript is the most important concept. Execution environment defines data variables or other functions have access to, determine their own behavior. Each execution environment has an associated variable object  (variable object), all variables and functions defined in the environment are stored in this object. While our code can not access the object, but the parser will use it in the background while processing the data.

  Global execution environment is the most peripheral of an execution environment. Depending on the ECMAScript implementation where the host environment, objects that represent the execution environment is not the same. In the web browser, global execution environment is considered to be the window object, so all global variables and functions are as properties and methods of the window object created. All code is an execution environment after the implementation of the execution environment is destroyed, save all variable and function definitions which also will be destroyed. Global execution environment until the application is launched - for example, will be destroyed when you close the page or browser.

  Each function has its own execution wake. When the stream enters performing a function, the function will be pushed into the ambient environment of a stack. And after the function executes, the stack will pop up its environment, returning control to the execution environment before.

  When the code is executed in an environment, it creates a scope chain variable objects (scope chain). The use of the scope chain, when ordered to ensure access to all variables and functions have access to the execution environment. The front of the scope chain, variable object is always the environment where the code currently executing. If this environment is a function, it is the active object (activation object) as a variable object. Activities at the beginning object contains only one variable, namely the arguments object. The next scope chain variable object comprises from environment, and then the next object from the next containing environment. This has been extended to apply to the global execution environment; execution environment variable object of the global scope chain is always the last object.

  Identifier search process along an identifier interpretation scope chain stage by stage. Search process always starts from the front of the scope chain, then backward stepwise back, know where to find so far identifiers.

 

Guess you like

Origin www.cnblogs.com/pingzi-wq/p/11408454.html