zoukankan      html  css  js  c++  java
  • lexical scoping vs. dynamic scoping

    http://en.wikipedia.org/wiki/Scope_(computer_science)#Lexical_scoping_vs._dynamic_scoping

    The use of local variables — of variable names with limited scope, that only exist within a specific function — helps avoid the risk of a name collision between two identically named variables. However, there are two very different approaches to the answering question: what does it mean to be "within" a function?

    In lexical scoping (or lexical scope; also called static scoping or static scope), if a variable name's scope is a certain function, then its scope is the program text of the function definition: within that text, the variable name exists, and is bound to the variable's value, but outside that text, the variable name does not exist. By contrast, in dynamic scoping (or dynamic scope), if a variable name's scope is a certain function, then its scope is the time-period during which the function is executing: while the function is running, the variable name exists, and is bound to its variable, but after the function returns, the variable name does not exist. This means that if function f invokes a separately defined function g, then under lexical scoping, function g does not have access to f's local variables (since the text of g is not inside the text of f), while under dynamic scoping, function g does have access to f's local variables (since the invocation of g is inside the invocation of f).

    x=1
    function g () { echo $x ; x=2 ; }
    function f () { local x=3 ; g ; }
    f # does this print 1, or 3?
    //echo $x # does this print 1, or 2?

    Consider, for example, the program at right. The first line, x=1, creates a global variable x and initializes it to 1. The second line, function g () { echo $x ; x=2 ; }, defines a function g that prints out ("echoes") the current value of x, and then sets x to 2 (overwriting the previous value). The third line, function f () { local x=3 ; g ; } defines a function f that creates a local variable x (hiding the identically named global variable) and initializes it to 3, and then calls g. The fourth line, f, calls f. The fifth line, echo $x, prints out the current value of x.
    So, what exactly does this program print? It depends on the scoping rules. If the language of this program is one that uses lexical scoping, then g prints and modifies the global variable x (because g is defined outside f), so the program prints 1 and then 2. By contrast, if this language uses dynamic scoping, then g prints and modifies f's local variable x (because g is called from within f), so the program prints 3 and then 1. (As it happens, the language of the program is Bash, which uses dynamic scoping; so the program prints 3 and then 1.)

  • 相关阅读:
    toPrimitive方法使用
    使用js导入Excel数据,转化为json,导出指定json,合并单元格为excel
    vue-router基本使用
    json另类使用
    z-index无效情况
    构造函数另类使用。
    在worker中使用offscreenCanvas
    使用git提交代码一条龙
    IntelliJ IDEA使用技巧一览表
    Android studio 常用快捷键
  • 原文地址:https://www.cnblogs.com/lambdatea/p/3378668.html
Copyright © 2011-2022 走看看