Javascript (五):Scope
Scope
In javaScript, scope is the context in which code is executed.Typically, scope is used to define the extent of information hiding—that is, the visibility(可见性) or accessibility(可访问性) of variables from different parts of the program.
- Global Scope
- Function Scope
- Block Scope
- Lexical Scope
- Dynamic Scope
Global Scope
The global scope is the last stop in the scope chain . Therefore, variables defined in the global scope can be accessed from anywhere . Danger is that any piece of code can change global variables for any reason at any time.
// g1 be created in global scope var g1 = 123; // global scope (function() { // g be created in global scope g2 = 123; })();
变量声明不推荐lack var。缺少var的变量被称为implied globals。 implied globals会污染全局环境、导致代码很难管理.
Read More...