JavaScript Execution Environment (execution context)
When the JS controller (control) into the executable code, the controller will enter an execution environment, the activities of the multiple execution environment constitutes the implementation of the environment stack, the top is the running execution environment, when the controller into a new execution environment, the new execution environment is put to the top of the stack. The execution environment contains the following three components: components purpose lexical environment execution Environment code created by identifier referenced by a lexical Environment object variable environment The code within the execution environment is a lexical environment object of a binding created by a variable expression and a function expression. This binding Specifies the value associated with the This keyword within the execution environment. Execution Environment when executing global code, using the Eval function, the execution environment is created when a function is executed. Create an execution environment in two steps, the first step is to initialize the execution environment, the following is the initialization of global Code and functions: 1, Global Code ECMA Global Execution Environment C initialization process: (1) Set the variable environment to the Global Environment (2) set the lexical environment to the Global Environment (3) to set this binding as a global object 2, enter function code when entering a function object F, the caller provides the THISARG, the caller provides the argumentslist, perform the following steps (1) to set the This:if (function is strict mode code) { this= Thisarg; }else if (thisarg===null| | thisarg=undefined) { this= Global Object}else if (Type (Thisarg!==object)) { this=toobject (THISARG);} else{ This=thisarg;} (2) The Internal property [[Scope]] of F is invoked as a parameter to invoke the declarative execution environment, and the result is localenv, setting the lexical environment to localenv, setting the variable environment to LOCALENV (3) to make the internal property of Code F [[Code]] The next step is to declare the binding initialization process, which is more complex and the main process is as follows: 1,env= the current execution Environment 2,if (eval code) { configurablebindings=true;} else{ CONFIGurablebindings=false; } 3,if (Strict mode code) { strict=true;} else{ Strict=false} 4,if (function code) { func= calls the internal properties of code [[call]] results in the form parameters of Names=func [[ Formalparameters]] Internal properties Argcount=args length n=0 foreach (argname in names) { &N Bsp n++ V=n>argscount?undefined:v=args[n] ARGALREADYD Eclared=env. Hasbinding (argname) if (argalreadydeclared===false) { Env. Createmutablebinding (argname)//variable environment create variable bindings } Env. Setmutablebinding (argname,v,strict)/variable environment set variable binding } 5, for each function in the code declaration F, follow these steps: fn= F's identifier FO=F initialization results FUNCALREADYDECLARED=ENV. Hasbinding (FN) if (funcalreadydeclared===false) { Env. Createmutablebinding (fn,configurablebindings) }else if (env=== Global Environment Record object) { go= Global object EX istingprop= The result if (existingprop [[configurable]] internal property = = = For the Go's internal property with FN true) { go.[ [Defineownproperty]] (Fn,property descriptor {[[Value]]: Undefined, [[writable]]: true, [[Enumerable]]: true, [[Configurable]]: Configurablebindings},true) }else if (Existingprop is an accessor property descriptor or Existingprop has no attribute value {[[writable] : true, [[Enumerable]]: true}) { Throw a TypeError exception &NBSP ; } Env. Setmutablebinding (fn,fo,strict) } 6,argumentsalreadydeclared=env. hasbinding (arguments) 7,if (Code is function code and Argumentsalreadydeclared===false) { argsobj= Createargumentsobject (func, names, args, env, strict) if (strict===true) { &NBsp Env. Createimmutablebinding (arguments) Env. Initializeimmutablebinding (arguments,argsobj)//Strict mode arguments immutable }else{ Env. Createmutablebinding (arguments) Env. Setmutablebinding (Arguments,args,false)//non-strict mode arguments variable } 8, declare D: dn= for each variable in code D's identifier varalreadydeclared=env. Hasbinding (DN) if (varalreadydeclared===false) { Env. Createmutablebinding (dn,configurablebindings) Env. Setmutablebinding (dn,undefined,strict) }