Then look at JavaScript execution context, variable object

Source: Internet
Author: User

Suddenly saw a 2010-year old article, the author in the form of chapters introduced to US ECMA-262-3 part of the content, mainly related to the execution context, variable object, scope, this and other language details. The content is short and lean, the style is straightforward and rigorous, reading has a hearty, clairvoyant feeling, highly recommended!!!

Original link: here

I want to translate the written, the original has already been done, here. Really untimely, how sorry ah!

Make a note and talk about my heart. Execution Context ExecutionContext

Whenever the controller (control) is converted to ECMAScript executable code, it is created and entered into an executable context.

A short sentence, but contains a wealth of content:

    • Controller: IE js engine
    • Conversion: Jumping from one executable code to another executable code
    • Executable code: Global Code, function code, eval code (corresponding to three scopes respectively)
    • Execution context: is an abstract concept, ECMA-262 standard uses this concept with the executable code (executable) concept to distinguish

The execution context logically composes a stack. The bottom of the stack is always the global context, and the top of the stack is the current/Active execution context (Activeexecutioncontext). The stack is modified at the same time that the variables of the EC type (various KINGDS of EC) are pushed in or ejected.

For example, we can define an array to emulate the execution context stack:

ECStack = [  globalContext,  <foo> functionContext]
Variable Object Variableobject

The variable object (VO) exists as a property of the execution context, and it stores the following contents:

  • All variable declarations (VAR, variabledeclaration)
    • A property of Vo, which consists of a variable name and a undefined value, and if the variable name is the same as a formal parameter or function that has already been declared, the variable declaration does not interfere with such an attribute that already exists.
  • function declaration (functiondeclaration, abbreviated as FD)
    • A property of Vo, which consists of the name and value of a function object (Function-object), and if the variable object already has a property of the same name, this property is completely replaced.
  • and the function's formal parameter
    • vo, a property that consists of the name and value of a formal parameter, and if no actual argument is passed, the attribute consists of the name of the formal parameter and the undefined value;
      VO = {//Context data (Var, FD, function arguments)}  

    When we declare a variable or a function, we also create a new property in Vo with the name and value of the variable.
    For example:

      var m = 30;function Test (A, b) {var c = 20function D () {}var e = function _e () {};} Test  

    When entering the context of the "test" function (passing parameters of ten), AO is as follows:

      AO (Test) = {a:10,b:undefined,c:undefined,d: <reference to Functiondeclaration "D" >e:undefined};  

    Test executes to the last, corresponding to the context stack at the moment:

      ecstack = [Globalcontext: {vo: {m:30, test:}},test functioncontext: {vo: {a:10, B:undef Ined, c:20, D: <reference to Functiondeclaration "D", and E: <reference to Functiondeclaration "_e" >} }]  
    about variables

    Typically, various articles and JavaScript-related books claim that a variable can be declared either using the var keyword (in the global context) or not using the var keyword (anywhere). Keep in mind that this is absolutely a rumor: at any time, a variable can only be declared by using the var keyword.

Let's take a look at the specific differences from the following examples:

alert(a); // undefinedalert(b); // "b" is not defined b = 10;var a = 20;

All the root causes are still VO and its modification phase (into the context phase and execution code phase):

VO = {  a: undefined};

As we can see, because "B" is not a variable, there is no "B" at this stage, and "B" will only appear in the execution code stage (but in our case, it is not there yet).

Let's change the example code:

alert(a); // undefined, we know why b = 10;alert(b); // 10, created at code execution var a = 20;alert(a); // 20, modified at code execution

There is also an important point of knowledge about variables. A variable has an attribute (attribute) relative to a simple property: {Dontdelete}, which means that the variable property is removed directly from the delete operator.

a = 10;alert(window.a); // 10 alert(delete a); // true alert(window.a); // undefined var b = 20;alert(window.b); // 20 alert(delete b); // false alert(window.b); // still 20

2018-8-2-Look again execution context, variable object

Then look at JavaScript execution context, variable object

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.