Since ECMA-/Javascript is all about Objects
and Contexts
(which, are also somekind of Object), every variable is stored in a such called Variable- (or in case of a Function, Activation Object ).(由于ECMA // Javascript都是关于Objects
和Contexts
(它们也是某种对象)的,因此每个变量都存储在所谓的Variable- (如果是Function,则为Activation Object )中。)
So if you create variables like this:(因此,如果您创建这样的变量:)
var a = 1,
b = 2,
c = 3;
In the Global scope (= NO function context), you implicitly write those variables into the Global object (= window
in a browser).(在全局范围 (= NO函数上下文)中,您将这些变量隐式写入全局对象 (=浏览器中的window
)。)
Those can get accessed by using the "dot" or "bracket" notation:(可以使用“点”或“括号”表示法来访问它们:)
var name = window.a;
or(要么)
var name = window['a'];
This only works for the global object in this particular instance, because the Variable Object of the Global Object is the window
object itself.(这仅在此特定实例中适用于全局对象,因为全局对象的变量 对象是window
对象本身。)
Within the Context of a function, you don't have direct access to the Activation Object .(在函数的上下文中,您没有直接访问Activation Object的权限。) For instance:(例如:)
function foobar() {
this.a = 1;
this.b = 2;
var name = window['a']; // === undefined
alert(name);
name = this['a']; // === 1
alert(name);
}
new foobar();
new
creates a new instance of a self-defined object (context).(new
创建一个自定义对象(上下文)的新实例。)
Without new
the scope of the function would be also global
(=window).(没有new
功能,该功能的范围也将是global
(=窗口)。) This example would alert undefined
and 1
respectively.(此示例将分别警告undefined
和1
。) If we would replace this.a = 1; this.b = 2
(如果我们将其替换this.a = 1; this.b = 2
) this.a = 1; this.b = 2
with:(this.a = 1; this.b = 2
其中:)
var a = 1,
b = 2;
Both alert outputs would be undefined.(两个警报输出均未定义。)
In that scenario, the variables a
and b
would get stored in the Activation Object from foobar
, which we cannot access (of course we could access those directly by calling a
and b
).(在这种情况下,变量a
和b
将存储在foobar
的激活对象中,我们无法访问它(当然,我们可以通过调用a
和b
直接访问它们)。) 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…