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.)

  • 相关阅读:
    ASP.NET 页面间数据传递的方法
    ASP.NET中实现页面间数据传递的方法
    C# 连接SQL数据库
    C# 通过url地址获取页面内容
    JS弹窗带遮蔽的功能
    C# Code First 实例学习
    CS窗体程序数据列表分页
    关于RDLC报表打印预览界面显示页码问号的问题
    C#Dictionary键值对取值用法
    分别获取一个字符串中的字母和数字
  • 原文地址:https://www.cnblogs.com/lambdatea/p/3378668.html
Copyright © 2011-2022 走看看