Shallow & Deep Binding - What would this program print?

半城伤御伤魂 提交于 2019-12-21 12:14:59

问题


I'm not sure how to do this...

function f1()
{
    var x = 10;
    function f2(fx)
    {
        var x;
        x = 6;
        fx();
    };

    function f3()
    {
        print x;
    };

    f2(f3);
};

For each of the following two binding methods, what would the program print? A) Shallow Binding B) Deep Binding

Thanks for the help!


回答1:


Deep/shallow binding makes sense only when a procedure can be passed as an argument to a function.

  • Deep binding binds the environment at the time a procedure is passed as an argument.
  • Shallow binding binds the environment at the time a procedure is actually called.

Deep binding.

Here f3() gets the environment of f1() and prints the value of x as 10 which is local variable of f1().

Shallow binding.

f3() is called in f2() and hence gets the environment of f2() and prints the value of x as 6 which is local to f2()




回答2:


• The environment of the call statement that enacts the passed subprogram (shallow binding)

• The environment of the definition of the passed subprogram (deep binding).

In some cases, the subprogram that declares a subprogram also passes that subprogram as a parameter. In those cases, deep binding and ad hoc binding are the same.




回答3:


Shallow binding : the environment of call statement that enacts the passed subprogram Deep binding : the environment of the definition of the passed subprogram Ad hoc binding : the environment of call statement that passed the subproblem as an actual parameter



来源:https://stackoverflow.com/questions/15550648/shallow-deep-binding-what-would-this-program-print

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!