Chapter 1: What is Scope?
第一章:什么是作用域
One of the most fundamental paradigms of nearly all programming languages is the ability to store values in variables, and later retrieve or modify those values. In fact, the ability to store values and pull values out of variables is what gives a program state.
在几乎所有编程语言中,储存值到变量中和稍后检索或改变这些值都是最基本的能力之一。事实上,这种存储值到变量和将值移除变量的能力才给了程序以“状态”。
Without such a concept, a program could perform some tasks, but they would be extremely limited and not terribly interesting.
没了这个概念的话,一个程序也能完成一些任务,但他们就会有很多地方被限制并且一点都不有趣了。
But the inclusion of variables into our program begets the most interesting questions we will now address: where do those variables live? In other words, where are they stored? And, most importantly, how does our program find them when it needs them?
但是把变量包含到我们的程序中带来的最有趣的问题是:这些变量在哪儿?或者说,他们存储在哪儿?最重要的是,当我们的程序需要这些变量的时候,它是如何找到他们的?
These questions speak to the need for a well-defined set of rules for storing variables in some location, and for finding those variables at a later time. We‘ll call that set of rules: Scope.
这个问题需要一个定义明确的规则,用于存储变量在某些位置,以及在之后再找出这些变量,我们可以把这个规则叫:作用域
But, where and how do these Scope rules get set?
但是,作用域规则在哪儿设置?怎么设置的?
Compiler Theory
编译原理
It may be self-evident, or it may be surprising, depending on your level of interaction with various languages, but despite the fact that JavaScript falls under the general category of "dynamic" or "interpreted" languages, it is in fact a compiled language. It is not compiled well in advance, as are many traditionally-compiled languages, nor are the results of compilation portable among various distributed systems.
这可能是不言而喻的,或者可能令人惊讶——取决于你融会贯通各种语言的能力,但是尽管JS属于一般范畴的动态型和解释型语言,实际上它却是一个编译型语言。它不是提前编译好的,因为有许多传统编译语言也不是各种分布式编译系统的产物。
But, nevertheless, the JavaScript engine performs many of the same steps, albeit in more sophisticated ways than we may commonly be aware, of any traditional language-compiler.
但是,尽管如此,JS引擎也执行着许多相同的步骤,尽管它使用的是一种更为复杂的方式——比我们可能意识到的任何传统编译语言
In traditional compiled-language process, a chunk of source code, your program, will undergo typically three steps before it is executed, roughly called "compilation":
在传统的编译语言过程中,一块源代码,也就是你的程序,会在执行前遵从典型的‘三步走’,也就是‘编译’
You Don't Know JS: Scope & Closures(翻译的试试)