JavaScript Foundation Advanced Advanced _ In-depth analysis of the enforcement environment and scope chain

Executive Environment

The enforcement environment defines other functions that the variable or function has the right access, which determines their respective behavior. Each execution environment has a variable object associated with it.

Variable Objects

All variables and functions defined in the environment are saved in this object.

Global Execute Environment

The global execution environment is the most peripheral execution environment. In a web browser, a global execution environment is considered a Window object, so all global variables and functions are created as the properties and methods of the Window object. Each function has its own execution environment.

Scope chain

A scope chain of a variable object is created when the code is executed in an environment. Uses: Ensure all variables and functions of the enforcement environment accessible.

Features: The front end of the scope chain is always a variable object where the currently executed code is located. If this environment is a function, the active object is used as a variable object. The active object is the most only included with the arguments object. The next variable object in the scope comes from the included environment, and then the next one is included in the environment.

VAR color = “blue”; function changeColor () {var aNotherColor = “red”; function swapcolors () {var tempcolor = noothercolor; annothercolor = color; color = Tempcolor;} swapcolors ();

As shown in the figure, the internal environment can be accessed through the scope chain.The external environment cannot access variables and functions in the internal environment. Each environment can search up the scope chain to query the variables and functions; however, you cannot search down.
   No block level scope 
declared variable

The variable using a VAR declaration will be automatically added to the closest environment. If the VAR keyword is omitted, it will be added to the global environment.

javascript基础进阶_深入剖析执行环境及作用域链 Function add (Num1, Num2) {var sum = Num1 + Num2; Return Sum;} Var Result = add (10, 10) Console.log (SUM)

SUM IS Not De defined

Function Add (NUM1, Num2) {SUM = Num1 + Num2; return sum;} var result = add (10, 10) console.log (sum)


The search process starts from the front end of the scope chain, and the upward-level query is the identifier that matches the given name, and finds to terminate the search. That is, if there is an identifier in the local environment, it is not used in the parent environment.
  VAR color = "red"; function getColor () {return color;} console.log (getColor ())  


VAR color = “red”; function getColor () {var color = “green”; return color;} console.log (getColor ())

Reference: “JavaScript Advanced Program Design (Third Edition)”
 The above JavaScript foundation Advanced _ In-depth analysis of the execution environment and the scope chain is the full content of Xiaobian sharing to everyone, I hope to give you a reference, I hope everyone will support Tumi Cloud.                      
© Copyright Notice
Just support it if you like
comment Grab the couch

Please log in to comment