全局上下文中的函数 [英] Functions in global context
问题描述
我理解,没有new关键字的函数将其所有属性都分配给全局上下文。但我看到一些好奇的行为,这一块Javascript代码:
I understand that functions called without the "new" keyword spit out all their properties on to the global context. But I am seeing some curious behavior, with this piece of Javascript code:
function Test3() {
var a=0;
this.inc = function() {
return ++a;
};
this.noInc = function() {
return a;
};
this.testRef = function() {
return this;
};
return {
inc: inc,
testRef: testRef,
noInc: noInc
};
}
var o = Test3(); // Put func properties on global context
var o2 = Test3(); // Put func properties on global context (replacing properties above??)
// Both "o" and "o2" maintain their own copy of "a" (closure)
alert("o: " + o.inc());
alert("o: " + o.inc());
alert("o: " + o.inc()); // Will output 3 (as expected)
alert(noInc()); // Output: 1 (This seems to not be affected by o.inc() calls - expected)
// However...
alert("o2: " + o2.inc());
alert("o2: " + o2.inc());
alert("o2: " + o2.inc());
alert("o2: " + o2.inc()); // Will output 4 (as expected)
alert(noInc()); // Will output 4 (seems to share with o2), but why?
alert(o === window); // false
alert(o.testRef() === o); // true (I thought testRef() would be on global context?)
alert(o.testRef() === window); // false (^^)
alert(o2 === window); // false
alert(o2.testRef() === o2); // true (I thought testRef() would be on global context?)
alert(o2.testRef() === window); // false (^^)
alert(testRef() === window); // true (How come this is here? Look at comments above)
-
当我们调用
var o = Test()
时,究竟发生了什么?由于new
关键字丢失,我认为>
里面Test3()
会引用窗口吗? o是什么意思?
When we call
var o = Test()
, what exactly happens here? What context doesTest()
execute in. Since thenew
keyword is missing, I believe,this
insideTest3()
would refer to window? What does "o" refer to? Is it simply a variable declared on the global context?
如果上述是真的,那么o和o2如何能够维护Test3的局部变量的单独副本一个。我知道我们在这里的行动已经结束,但是怎么来,o2和窗口共享变量a的相同副本,但不是o
If above is true, then how are o and o2 able to maintain separate copies of Test3's local variable "a". I understand that we have closure in action here, but then how come, "o2" and "window" share the same copy of the variable "a", but NOT "o"
当我做var o = Test3()然后做alert(o.testRef()===窗口),它说false。所以执行后:
When I do var o = Test3() and then do alert(o.testRef() === window), it says false. So after executing:
var o = Test3();
var o2 = Test3();
似乎有3个副本的属性从 Test3()
。一个在o,另一个在o2,另一个在全局上下文。
There seems to be 3 copies of the properties from Test3()
. One on "o", another one on "o2" and one on the global context.
但是如何在o和o2我不是用new关键字调用 Test3()
,所以这应该只引用全局上下文?
But how can there be any on "o" and "o2" - I am not calling Test3()
with the "new" keyword, so this should only refer to global context?
推荐答案
每次调用 Test3
时, inc
, noInc
和 testRef
函数重新分配给窗口
> a
Every time you call Test3
, the inc
, noInc
and testRef
functions are reassigned to window
with a different a
(which is initialized to 0
at the top of the function.)
0 > 窗口
和 o2
共享相同的 a
当 o2
获得返回值而不是 o1 $ c $时,已重新分配给
c>。
window
and o2
share the same a
because the functions were reassigned to window
when o2
got the return value and not o1
.
来自NodeJS REPL的示例(注意在NodeJS中,全局对象由全局
引用,而不是 window
,但是不应该有任何区别):
Example from NodeJS REPL (note in NodeJS, the global object is referenced by global
instead of window
, but otherwise there shouldn't be any difference):
> function Test3() {
... var a=0;
...
... this.inc = function() {
... return ++a;
... };
...
... this.noInc = function() {
... return a;
... };
...
... this.testRef = function() {
... return this;
... };
...
... return {
... inc: inc,
... testRef: testRef,
... noInc: noInc
... };
... }
undefined
> var o = Test3();
undefined
> o.inc()
1
> o.inc()
2
> o.inc()
3
> noInc()
3
因为 Test3
只调用一次,并设置为 o1
,全局范围和 o1
共享 a
。现在观察,再次调用 Test3
:
Because Test3
was only called once and set to o1
, both the global scope and o1
share an a
. Now watch as I call Test3
again:
> Test3()
{ inc: [Function],
testRef: [Function],
noInc: [Function] }
> noInc()
0
> o.noInc()
3
函数重新分配给全局对象, a
。 0
,而 o
c $ c> a
The functions are reassigned to the global object, with an a
of 0
, while o
keeps the previous a
.
这篇关于全局上下文中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!