Scope
在programming language中,name的一个非常重要的属性就是它的scope,目前我们所遇到的programming language,比如Python、C++等,大多数采用的是static scope。
维基百科Scope (computer science)
In computer programming, the scope of a name binding – an association of a name to an entity, such as a variable – is the region of a computer program where the binding is valid: where the name can be used to refer to the entity. Such a region is referred to as a scope block. In other parts of the program the name may refer to a different entity (it may have a different binding), or to nothing at all (it may be unbound).
The scope of a binding is also known as the visibility of an entity, particularly in older or more technical literature – this is from the perspective of the referenced entity, not the referencing name. A scope is a part of a program that is or can be the scope for a set of bindings – a precise definition is tricky(棘手), but in casual use and in practice largely corresponds to a block, a function, or a file, depending on language and type of entity. The term "scope" is also used to refer to the set of all entities that are visible or names that are valid within a portion of the program or at a given point in a program, which is more correctly referred to as context or environment.[a]
Strictly speaking[b] and in practice for most programming languages, "part of a program" refers to "portion of the source code (area of text)", and is known as lexical scope(词法范围). In some languages, however, "part of a program" refers to "portion of run time (time period during execution)", and is known as dynamic scope(动态范围). Both of these terms are somewhat misleading – they misuse technical terms, as discussed in the definition – but the distinction itself is accurate and precise, and these are the standard respective terms. Lexical scope is the main focus of this article, with dynamic scope understood by contrast with lexical scope.
In most cases, name resolution based on lexical scope is relatively straightforward to use and to implement, as(因为) in use one can read backwards(向后读取) in the source code to determine to which entity a name refers, and in implementation one can maintain a list of names and contexts when compiling or interpreting a program. Difficulties arise in name masking, forward declarations, and hoisting, while considerably subtler ones arise with non-local variables, particularly in closures.
NOTE:通过identifier来refer object,那就出现这样的问题:哪些地方可以使用这个identifier;如果从name binding的角度来看的话,哪些地方可以binding,