Detailed description of the javascript Execution Environment

Source: Internet
Author: User

Javascript execution environment (Execution context)

When the Javascript controller (control) enters the executable code, the Controller enters an execution environment. Multiple execution environments of the activity constitute the execution environment stack, and the top is the running execution environment, when the Controller enters a new execution environment, the new execution environment is placed at the top of the stack. The execution environment includes the following three components: the identifier created by the code in the execution environment of the lexical environment refers to a lexical environment object. The code in the execution environment is a lexical environment object bound to the Code created by the variable expression and function expression.. This binding specifies the value associated with the this keyword in the execution environment. Execution environment creation when the global code is executed and an eval function is used to execute a function, the execution environment is created. Two steps are taken to create an execution environment. The first step is to initialize the execution environment. The following is the initialization process of the global code and function: 1. the initialization process of the global code ECMA global execution environment C: (1) set the variable environment to the Global Environment (2) set the lexical environment to the Global Environment (3) set this binding to the Global Object 2, enter the function code, and then enter a function object F, the thisArg provided by the caller. When the caller provides argumentsList, perform the following steps (1) to set this: if (the function is in 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 attribute [Scope] of F is called as a parameter to call the declarative execution environment. The result is recorded as localEnv, the lexical environment is set to localEnv, and the variable environment is set to localEnv (3) the internal attribute [[code] with Code as F is followed by the declaration of the binding initialization process. This process is complex. The main process is as follows: 1. env = variable environment 2 of the current execution environment, if (eval code) {configurableBindings = true;} else {configurableBindings = false;} 3, if (strict mode code) {strict = true;} else {strict = false ;} 4. if (Function code) {func = Call code's internal attribute [[Call] result names = func format parameter [[Forma LParameters] the internal attribute argCount = length of args n = 0 foreach (argName in names) {n ++ v = n> argsCount? Undefined: v = args [n] argAlreadyDeclared = env. hasBinding (argName) if (argAlreadyDeclared = false) {env. createMutableBinding (argName) // variable environment Creation variable binding} env. setMutableBinding (argName, v, strict) // set variable binding in the variable environment} 5. For each function declaration f in the code, follow these steps: fn = f Identifier fo = f initialization result funcAlreadyDeclared = env. hasBinding (fn) if (funcAlreadyDeclared = false) {env. createMutableBinding (fn, configurableBindings)} else if (env === Global Environment Record object) {go = Global Object existingProp = result if ([[GetProperty] of [[GetProperty] of the internal attribute of existingProp called with fn as the parameter = true) {go. [[DefineOwnProperty] (fn, Property Descriptor {[Value]: undefined, [[Writable]: true, [[Enumerable]: true, [[Writable able]: configurableBindings}, true)} else if (existingProp is the accesser attribute descriptor or existingProp has no attribute value {[Writable]: true, [[Enumerable]: true}) {Throw a TypeError exception} 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) {env. createImmutableBinding (arguments) env. initializeImmutableBinding (arguments, argsObj) // The arguments in strict mode is unchangeable} else {env. createMutableBinding (arguments) env. setMutableBinding (arguments, args, false) // non-strict mode arguments variable} 8. For each variable in the code, declare d: dn = d Identifier varAlreadyDeclared = env. hasBinding (dn) if (varAlreadyDeclared = false) {env. createMutableBinding (dn, configurableBindings) env. setMutableBinding (dn, undefined, strict )}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.