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。

  • 相关阅读:
    hdu5587 BestCoder Round #64 (div.2)
    hdu5569 BestCoder Round #63 (div.2)
    hihocoder1257(构造)(2015北京ACM/ICPC)
    hihocoder 1249(2015ACM/ICPC北京)
    hihocoder1258(水)(2015ACM/ICPC北京站)
    hihiocoder 1255(搜索)(2015ACM/ICPC北京站)
    习题9-8 uva1631
    习题9-8 Uva1632
    Orz
    习题9-6 uva 10723
  • 原文地址:https://www.cnblogs.com/jiangtian/p/6508427.html
Copyright © 2011-2022 走看看