Javascript Execution Context Context&scope

Source: Internet
Author: User

The execution context (execution context) execution context can be thought of as the execution environment of the code. 1 when the code is loaded, the JS interpreter creates a global execution context. 2 The execution context of a function is created when the function is executed. 3 when performing eval (), create an eval execution context. # If,for,while blocks do not create a execution context, so the scope is not created.   When JS interpreter starts to work: 1 First create an execution context stack (LIFO) 2 Then create a global execution context and put it into the execution context stack. 3 When a function is called in the global context .    creates an execution context for the function,    presses into the execution context stack. 4 The execution context at the top of the stack is ejected when the function execution is finished. Resumes execution of the new execution context at the top of the stack.   This approach guarantees that only the execution context at the top of the stack will be executed at any one time, that is, a single thread is implemented.    execution context creation is divided into two stages:    creation phase: All variables, function declarations, and function parameters are composed of an active object (Mutable object)     Execution phase: Interpreting Code .  What to do during the creation phase: 1 Create the active object Vo (variableobject) 2 Create arguments object     Detect parameter, take the parameter name as the key value (all properties of the arguments object are both VO properties) 3 detects a function declaration in the current execution context (function fn () {} This is called a functional declaration)   Each detection of a function declaration adds a property to the VO, and the property name is the function name. The property value is the function's in-memory reference   (      detects that the same name is still overwritten.)  ) 4 detection variable declaration   each variable declaration is detected, add a property on the Vo, the property name is the variable name, the property value is undefined.  (      The same name is detected to discard the current.  ) #34的执行顺序没有关系的, the same result is the order of exchange. As long as the variable declaration meets the function declaration, the last one left is definitely the function declaration. 5 determines the point of the current this.   Execution stage: one line to interpret the code .  butThere is another rule in the line stage: for the assignment of the same variable, the following will overwrite the previous one.    Suppose there is a function:  functions foo (i) {   var a = ' Hello ';   var b = function privateb () { &NB Sp  };   function C () {    }}vo probably looks like this: ExecutionContext = {   variableobject: {& nbsp      arguments: {           0:22,           le ngth:1       },       i:22,       c:pointer to function C ()        a:undefined,       b:undefined   },   scopechain: {. .. },   this: {...}} The   scope CHAINJS interpreter creates a scope chain at the same time as the execution context stack is created. A one-way list. The end of the creation phase of an execution context: the activity object that is created is placed in the first position of the scope chain. When executing the execution phase of the context, when a variable needs to be accessed, it is first looked up in the VO of the current execution context. If it is not found, it is searched for the next position in the list until the last position. # because the list is unidirectional and cannot be reversed, this is why the global execution context cannot access the function context, which is why the function cannot be accessed outside the function.   1 triggers the scope chain lookup for only one reason: the execution phase of the execution context, Met the current VA property that does not exist in O. So:    var a = ' outer ';    function test (a) {        Console.log (a);//(1)   &NB Sp     var a = ' inner ';        Console.log (a);//(2)    }    test ();  & nbsp output:undefined, ' inner ' because the parameter is in A>vo with attribute A, at (1), the value of a is undefined, because a is present, so the scope chain lookup is not triggered. If you want to trigger, change the parameter A to a different name. . # this thing, to help understand the principle of good, do not in the code to do so, out of the question do not know where there is a problem.  2 This is used to prove the search direction of scope chain.     var x = 10;     function foo () {      alert (x);   }   &NB Sp (function () {      var x = 20;      foo ();   }) ();  3//duplicate function declarations, followed by overrides (function declaration comparison overbearing point) function f () {    Console.log (1);}  f (); function f () {    Console.log (2);} f ();  4 the law of the execution phase, the subsequent assignment overrides the previous name. var x = 0;var F = function () {    x = 1;} f (); alert (x); function f () {    x = 10;} f (); alert (x);//Output:1,1  var B = ten; (fUnction B () {  B = 20;    Console.log (b);}) ();//output:function B () {...} Cause: 1 First enters the global execution context, registers the B2 global execution context to execute B = 103 enters () (), creates the execution context, registers the function declaration B4 executes-enters the function execution Context 5 registers B is global. 6 output:  not found in function, found in expression, is function B () {}  if: var B = ten; (function B () {  var b = 20;  Console.log (b);} ) ();//output:20  if: var B = ten; (function () {  B = 20;  Console.log (b);}) ()///Output:20 iife and the global execution context also have a layer of execution context in order to explain the above problem.    function Expression & function declaration     VAR fn = function () {}//function expression     function fn () {} //function declaration   Memory: VB: The use of Var is biao   difference: function declaration must have the function name, only the function declaration has Hoisting.  javascript hositing variables and function declarations are always promoted to the top of the current scope. Cause: During the creation phase of the execution context, all variable declarations and function declarations have been detected when the Vo object is created. So they already exist at this time. Only the value of the variable declaration is always undefined. The value of a function declaration is a reference to a function in the memory address.

Javascript Execution Context Context&scope

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.