本地/全球范围的混乱 [英] local/global scope confusion

查看:56
本文介绍了本地/全球范围的混乱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨组,


考虑这个简单的脚本(在FF3上测试):


< script type =" text / javascript" ;>

test =''outer'';

for(var i = 0; i< 2; i ++){

alert (测试);

var test =''inner'';

alert(test);

}

警报(测试);

< / script>

我预计它会发出警报:

(i == 0):外部内在

(i == 1):????内部

(循环后):外部


我只是很好奇,想知道是什么????会是。

(事实证明它是内心的。)


但最令我惊讶的是最后一次警报,即/>
forloop,也给了我''内部''。

所以看起来声明我的本地测试变量会影响全局的

测试变量。我不明白。


有人可以向我澄清为什么会这样吗?

我必须遗漏一些东西。

TIA!


问候,

Erwin Moller


-

有两种构建软件设计的方法:一种方法是

使它变得如此简单以至于显然没有任何缺陷,并且

其他方式是为了使它变得如此复杂以至于没有明显的b $ b缺陷。第一种方法要困难得多。

- C.A.R. Hoare

Hi group,

Consider this simple script (tested on FF3):

<script type="text/javascript">
test = ''outer'';
for (var i=0;i<2;i++){
alert(test);
var test = ''inner'';
alert (test);
}
alert (test);
</script>
I expected it would alert:
(i==0): outer inner
(i==1): ???? inner
(after loop): outer

I was just curious and wondered what the ???? would be.
(It turns out that it is inner.)

But what surprised me most was that the last alert, the one after the
forloop, also gave me ''inner''.
So it looks like declaring my local test variable affects the global
test variable. I don''t get that.

Can anybody clarify to me why this happens?
I must be missing something.

TIA!

Regards,
Erwin Moller

--
"There are two ways of constructing a software design: One way is to
make it so simple that there are obviously no deficiencies, and the
other way is to make it so complicated that there are no obvious
deficiencies. The first method is far more difficult."
-- C.A.R. Hoare

推荐答案

Erwin Moller meinte:
Erwin Moller meinte:

Hi group,


考虑这个简单的脚本(在FF3上测试):


< script type =" text / javascript">

test =''outer'';

for(var i = 0; i< 2; i ++){

alert(test);

var test =''inner'';

alert(test);

}

alert(test);

< / script>


我预计它会提醒:

(i == 0):外部内部

(i == 1):????内部

(循环后):外部


我只是很好奇,想知道是什么????会是。

(事实证明它是内在的。)
Hi group,

Consider this simple script (tested on FF3):

<script type="text/javascript">
test = ''outer'';
for (var i=0;i<2;i++){
alert(test);
var test = ''inner'';
alert (test);
}
alert (test);
</script>
I expected it would alert:
(i==0): outer inner
(i==1): ???? inner
(after loop): outer

I was just curious and wondered what the ???? would be.
(It turns out that it is inner.)



JavaScript具有功能范围,而不是块范围。 (

ECMAScript的未来版本可能允许块范围确定。)

JavaScript has function scope, not block scope. (Future versions of
ECMAScript might allow block scoping, though.)


但最令我惊讶的是最后一个警报,一个在

forloop之后,还给了我''内部''。

所以看起来声明我的本地测试变量会影响全局

测试变量。我不明白。
But what surprised me most was that the last alert, the one after the
forloop, also gave me ''inner''.
So it looks like declaring my local test variable affects the global
test variable. I don''t get that.



因为你总是在同一范围内,所以你总是使用

相同的变量。

Since you are always in the same scope, you are always working with the
same variable.


有人可以向我澄清为什么会这样吗?

我必须遗漏一些东西。
Can anybody clarify to me why this happens?
I must be missing something.



是的。当前版本的JavaScript中没有块范围。


Gregor

Yes. There is no block scope in the current versions of JavaScript.

Gregor


Gregor Kofler schreef:
Gregor Kofler schreef:

Erwin Moller meinte:
Erwin Moller meinte:

>嗨组,

考虑这个简单的脚本(在FF3):

< script type =" text / javascript">
test =''outer'';
for(var i = 0; i< 2 ; i ++){
警报(测试);
var test =''inner'';
警报(测试);
}
警报(测试);
< / script>

我预计它会发出警报:
(i == 0):外部内部
(i == 1):????内部(循环后):外部

我只是好奇,想知道是什么????会是。
(事实证明它是内在的。)
>Hi group,

Consider this simple script (tested on FF3):

<script type="text/javascript">
test = ''outer'';
for (var i=0;i<2;i++){
alert(test);
var test = ''inner'';
alert (test);
}
alert (test);
</script>
I expected it would alert:
(i==0): outer inner
(i==1): ???? inner
(after loop): outer

I was just curious and wondered what the ???? would be.
(It turns out that it is inner.)



JavaScript具有功能范围,而不是块范围。 (

ECMAScript的未来版本可能允许块范围确定。)


JavaScript has function scope, not block scope. (Future versions of
ECMAScript might allow block scoping, though.)


>但最令我惊讶的是最后一次警报,
forloop之后的那个,也给了我''内部''。
所以看起来声明我的本地测试变量会影响全局的测试变量。我不明白。
>But what surprised me most was that the last alert, the one after the
forloop, also gave me ''inner''.
So it looks like declaring my local test variable affects the global
test variable. I don''t get that.



因为你总是在相同的范围内,所以你总是使用

相同的变量。


Since you are always in the same scope, you are always working with the
same variable.


>有人可以向我澄清为什么会发生这种情况吗?
我一定会遗漏一些东西。
>Can anybody clarify to me why this happens?
I must be missing something.



是的。当前版本的JavaScript中没有块范围。


Gregor


Yes. There is no block scope in the current versions of JavaScript.

Gregor



谢谢Gregor。


我想知道为什么我多年来都没有注意到我使用JavaScript。 ;-)

/ me自拍。


以下脚本清楚地显示了您所说的内容。


< script type =" text / javascript">

var test =''outer'';

function aFunc(){

alert(test);

for(var i = 0; i< 2; i ++){

var test =''inner'';

提醒(测试);

}

}

aFunc();

提醒(测试) ;

< / script>


给出:undefined,inner,inner,outer。

正如所料(一旦你知道)目前的JavaScript没有块范围。


谢谢。


问候,

Erwin Moller

-

有两种构建软件设计的方法:一种方法是使b
使它变得如此简单以至于显然没有任何缺陷而另一种方式是让它变得如此复杂以至于没有明显的b $ b缺陷。第一种方法要困难得多。

- C.A.R. Hoare

Thanks Gregor.

I wonder why I never noticed in all the years I use JavaScript. ;-)
/me goes shoot himself.

The following script clearly shows what you said.

<script type="text/javascript">
var test = ''outer'';
function aFunc(){
alert(test);
for (var i=0;i<2;i++){
var test = ''inner'';
alert (test);
}
}
aFunc();
alert (test);
</script>

gives: undefined, inner, inner, outer.
As expected (once you know there is no block scope in current JavaScript).

Thanks.

Regards,
Erwin Moller
--
"There are two ways of constructing a software design: One way is to
make it so simple that there are obviously no deficiencies, and the
other way is to make it so complicated that there are no obvious
deficiencies. The first method is far more difficult."
-- C.A.R. Hoare


Gregor Kofler写道:
Gregor Kofler wrote:

当前版本的JavaScript中没有块范围。
There is no block scope in the current versions of JavaScript.



ECMAScript没有它,但是Mozilla的JavaScript具有块范围

自版本1.7以来,请参阅
https://developer.mozilla.org/en/New ... scope_with_let

-


Martin Honnen
http://JavaScript.FAQTs.com/


这篇关于本地/全球范围的混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆