local/global scope confusion

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
 
G

Gregor Kofler

Erwin Moller meinte:
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 has function scope, not block scope. (Future versions of
ECMAScript might allow block scoping, though.)
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.

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

Gregor
 
E

Erwin Moller

Gregor Kofler schreef:
Erwin Moller meinte:

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


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


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

Gregor

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
 
T

Trevor Lawrence

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

I thought I understood the scoping of JavaScript, but now I don't

So a variable declared and modified outside a function is not known inside
the function ?
But if a variable is declared and modified outside a function, it can be
modified inside the function and its value is then known outside the
function ??

For example
<script type="text/javascript">
var test1 = 'outer1'; var test2 = 'outer2';
alert('test1 ' + test1); alert('test2 ' + test2);

function aFunc(){
alert('test1 ' + test1); alert('test2 ' + test2);
for (var i=0;i<2;i++){
var test1 = 'inner1';
test2 = 'inner2';
alert('test1 ' + test1); alert('test2 ' + test2);
}
}
aFunc();
alert('test1 ' + test1);alert('test2 ' + test2);
</script>

gives these alerts
test1 outer1 ]
test2 outer2 ] Outside function

test1 undefined ]
test2 outer2 ] Inside function - before loop

test1 inner1 ]
test2 inner2 ] Inside function - inside loop

test1 inner1 ]
test2 inner2 ] Inside function - inside loop

test1 outer1 ]
test2 inner2 ] Outside function

Why is the variable test1 undefined at the start ?

Is the function parsed before being executed so that JS knows that test1 is
local, but is not actually defined until a line or so later?

BTW,
I may be that some explanations of the *meaning* of block scope and function
scope could be useful here, and not just "JavaScript does not have block
scope"
 
E

Evertjan.

Trevor Lawrence wrote on 16 nov 2008 in comp.lang.javascript:
I thought I understood the scoping of JavaScript, but now I don't

So a variable declared and modified outside a function is not known
inside the function ?

No, and Yes:

A variable declared outside a function is not known inside the function,
only if a variable with the same name is declared inside that function.

Javascript is not a linear parser, the parsing can be thougt of as a two
phase process. In the first phase the names of variables and functions
are defined with their scope, the second phase is the execution.

[In fact the first phase probably also makes and optimizes an
intermediate code.]

The modification has nothing to do with is, but identifing these facts in
your test.

There are two different variables in the below code, both are named a.
One has a global scope, the othe a scope local to f1().

<script type="text/javascript">

var a = 1;

document.write('<br>' + a); // 1 [global a]

function f1(){
document.write('<br>' + a); // 1 [global a]
};

function f2(){
document.write('<br>' + a); // undefined [local scope a]
var a = 2;
document.write('<br>' + a); // 2 [local scope a]
};

f1();
f2();
f1(); // 1 [global a]

</script>
 
T

Trevor Lawrence

Thanks Evertjan and Jorge
Your answers gave precisely the info. I needed, even though my question may
have been a little unclear.

I have noticed that many other replies in this NG have not been as polite
and helpful

--
Trevor Lawrence
Canberra
Web Site http://trevorl.mvps.org

Is the function parsed before being executed so that JS knows that test1
is
local, but is not actually defined until a line or so later?

Yes, that's it.
 
T

The Natural Philosopher

Trevor said:
Thanks Evertjan and Jorge
Your answers gave precisely the info. I needed, even though my question may
have been a little unclear.

I have noticed that many other replies in this NG have not been as polite
and helpful
One of the reasons I don't normally hang out here.

There are two people who actually really help..Erwin Moller is one, and
I forget the other..

There are about 3-4 who try, but often don't really understand the problem.

The rest are probably here because they can't actually write javascript,
(IMHO an entirely reasonable thing) but like to think they can (entirely
unreasonable !)

Personally I find it a total bitch after C.

Its loosely typed with respect to variables, but much stronger with
respect to object data versus method..for example.

The DOM is very weird.

There are some abbreviations that work, and some that don't.
You can name some elements, bit not others.

Some DOM descriptions parts vary from browser to browser. I found the
worts was the way in which <SELECT><OPTION> elements responded to
mouseover events..fortunately you don't need those with a full js
implementation, you can create your own flyout selections.

<INPUT type FILE> stuff is an abortion with respect to styling..but
thats not javascripts fault. You can patch your way round that by laying
dummy layers over the top to mask the inherent ugliness..

Oh, and the way in which elements are stored in the DOM varies between
browsers as well..
 
E

Evertjan.

The Natural Philosopher wrote on 17 nov 2008 in comp.lang.javascript:
The DOM is very weird.

The DOM is not part of Javascript.

Perhaps you mean the DOM-Javascript interface, but in IE that is the same
one as the DOM-VBS interface, so not Javascript specific.

<INPUT type FILE> stuff is an abortion with respect to styling..

An abortion?
An abomination perhaps, but this does not touch Javascript at all.

Better switch to an HTML NG.

;-)
 
T

The Natural Philosopher

Evertjan. said:
The Natural Philosopher wrote on 17 nov 2008 in comp.lang.javascript:

The DOM is not part of Javascript.

Perhaps you mean the DOM-Javascript interface, but in IE that is the same
one as the DOM-VBS interface, so not Javascript specific.

Yup. See what I mean? where does the DOM stop and javashite begin?
An abortion?
An abomination perhaps, but this does not touch Javascript at all.

Better switch to an HTML NG.

see point above. Why doesn't javascript have PROPER access to these
elements?

Indeed...
 
E

Evertjan.

The Natural Philosopher wrote on 17 nov 2008 in comp.lang.javascript:
Yup. See what I mean? where does the DOM stop and javashite begin?

Where does the land stop, and the sea begin?

Javascript is a far more general language than a clientside browser one.

Don't blame the German language for Hitler's behavour.
[Though I often would like to]

The DOM was not invented to accomodate Javascript.
see point above. Why doesn't javascript have PROPER access to these
elements?

Because the browser does not provide it, not a Javascript issue, but a
sensible one securitywize.
 
T

The Natural Philosopher

Evertjan. said:
The Natural Philosopher wrote on 17 nov 2008 in comp.lang.javascript:
Yup. See what I mean? where does the DOM stop and javashite begin?

Where does the land stop, and the sea begin?

Javascript is a far more general language than a clientside browser one.

Don't blame the German language for Hitler's behavour.
[Though I often would like to]

The DOM was not invented to accomodate Javascript.
see point above. Why doesn't javascript have PROPER access to these
elements?

Because the browser does not provide it, not a Javascript issue, but a
sensible one securitywize.

Theres a difference between being able to determine attributes of style,
and being able to rob the user of his files...
 
E

Evertjan.

The Natural Philosopher wrote on 17 nov 2008 in comp.lang.javascript:
Theres a difference between being able to determine attributes of style,
and being able to rob the user of his files...

Style is not Javascript, you could try CSS, but do reply in a PROPER NG.
 
T

The Natural Philosopher

Evertjan. said:
The Natural Philosopher wrote on 17 nov 2008 in comp.lang.javascript:


Style is not Javascript, you could try CSS, but do reply in a PROPER NG.
such a nice friendly group. Just like the language.
 
J

Jorge

One of the reasons I don't normally hang out here.

There are two people who actually really help..Erwin Moller is one, and
I forget the other..

There are about 3-4 who try, but often don't really understand the problem.

The rest are probably here because they can't actually write javascript,
(IMHO an entirely reasonable thing) but like to think they can (entirely
unreasonable !)

Yeah, the normal, the abnormal and the ones who imitate the abnormal.
Normal people would never dare to spit in the first place, in the face
of someone whom you don't know at all an "are you too lazy or too
stupid ?". That's absolutely disrespectful and crude and inexcusable
and abnormal. Sure, not big enough eggs to behave so face to face, but
parapeted far away behind the network... in a word: a psychopathology.
 
T

The Natural Philosopher

Jorge said:
Yeah, the normal, the abnormal and the ones who imitate the abnormal.
Normal people would never dare to spit in the first place, in the face
of someone whom you don't know at all an "are you too lazy or too
stupid ?". That's absolutely disrespectful and crude and inexcusable
and abnormal. Sure, not big enough eggs to behave so face to face, but
parapeted far away behind the network... in a word: a psychopathology.

I remember the days of Usenet, when you would, via a 9600 modem,ask.

"how the heck to I get my printer working on Sys4 Unix?

and somebody would cut and paste or retype - the appropriate config from
their working setup.

bandwidth was almpost to low for abuse, as were latencies..to high.

Of course we are ignorant: that's why we are asking.

Lazy and stupid?..not many.


 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top