zoukankan      html  css  js  c++  java
  • 深度剖析典型函数内部作用域链的动态变化

    function factory() {
         var name = 'laruence';
         var intro = function(){
              alert('I am ' + name);
         }
         return intro;
    }
     
    function app(para){
         var name = para;
         var func = factory();
         func();
    }
     
    app('eve');

    contentStack = {

        globalContext
    }

    globalContext = {

        globalariableObject : [factory,app],

        globalScopeChain : globalariableObject

    }

    app.[[Scope]] = globalScopeChain;

    ----------------------------------------------

    contentStack = {

        appContext,
        globalContext
    }

    appContext = {

        appariableObject : [arguments,para,name,func,factory,app],

        appScopeChain : [appariableObject, app.[[Scope]]]

    }

    factory.[[Scope]] = appScopeChain;

    -----------------------------------------------

    contentStack = {

        factoryContext,

        appContext,

        globalContext

    }

    factoryContext  = {

       factoryariableObject : [arguments, name, intro,factory],

       factoryScopeChain : [factoryariableObject, factoryScope]

    }

    intro.[[Scope]] = factoryScopeChain;

    ------------------------------------------------

    contentStack = {
        introContext,

        factoryContext,

        appContext,

        globalContext

    }

    introContext = {

        introariableObject = [arguments,name,intro],

        introScopeChain = [introariableObject, intro.[[Scope]]]

    }

    因此最终的执行结果是intro可以访问到的name,也即laruence。

  • 相关阅读:
    POJ
    CodeForces
    部分和问题
    NOIP200502校门外的树
    消灭虫子
    抓牛
    最长不下降子序列的长度
    HDNoip201501计算结果最小
    抽签
    Ants
  • 原文地址:https://www.cnblogs.com/jiangtian/p/6508427.html
Copyright © 2011-2022 走看看