Well... only one way to find out!
Here's what I tried:
<html>
<head>
<title>Untitled</title>
<script language="JavaScript">
var mytest = true;
function ShowNow() {
var mytest = null;
alert( "Now: " + mytest );
mytest = false;
}
function ShowAfter() {
alert( "After: " + mytest );
mytest = true;
}
</script>
</head>
<body>
<a href="javascript: ShowNow();">Show Now</a><br>
<a href="javascript: ShowAfter();">Show After</a>
</body>
</html>
When you click on Show Now it gives null... so mytest is found in the local
space for the function and used. Show After doesn't declare mytest anywhere
in the local section and therefore goes to the global space and finds it
there.
Is this how it also works in C/C++ and other such languages?
Eric
Scott Andrew LePera Wrote:
Usually, using "var" in your declaration makes the variable local to
the function or area where it was declared.
I may be incorrect, but in my experience, the only way to make a
variable truly global is to NOT use var:
Wrong:
var x=true
function changeIt(){
x=false // <-- will cause error, because x is local outside of
function
}
Right:
x=true // <-- variable is now global
function changeIt(){
x=false
}
Has anyone had a different result?
scottandrew
_________________________________________________________________________
Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com.
Share information about yourself, create your own public profile at
http://profiles.msn.com.
|