E
Erwin Moller
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
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