js execution environment and the scope chain

Execution Environment

js execution environment is to: define data variables or other functions have access to, determine their own behavior. Each environment has associated with it a target, variables and functions defined in the environment are stored in this object.

 

Global variables is the most peripheral environment, generally considered to be the window object. Global variables and local variables in a stack environment, when the execution environment After all code execution environment will be destroyed, environmental stack will pop the stack, variables and functions defined are also destroyed, and the global variables in the program destroyed after exit.

The scope chain

Scope is to ensure orderly access to all variables have access to the execution environment.

Chain is formed:

The front end of the current scope chain variable object execution environment, the next layer is the current execution environment of the external environment, the external environment is lower external environment until the current execution environment ..... outermost layer is global.

Looking for an identifier, starting with the variable object forefront of the scope chain began to find, did not find it down a find, until the global, if not found on the error.

Extend the scope chain:

Extension of the scope chain is to the front end of a temporary increase in the scope of a variable object, such statement in two ways to achieve: try-catch in the catch, with.

catch: create a new variable object containing error objects thrown.

with: Adds the specified object to the scope.

 

Reference: "Javascript advanced programming," third edition

Guess you like

Origin www.cnblogs.com/ellen-mylife/p/11259441.html