Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > variable scope

Reply
Thread Tools

variable scope

 
 
Reg@nospam.com
Guest
Posts: n/a
 
      07-05-2011
var scope = "global scope";
function checkscope(){
var scope = "local scope";
document.write(scope);
}
checkscope();
document.write(scope);

as you would expect results in
local scopeglobal scope

while

var scope = "global scope";
function checkscope(){
scope = "local scope";
document.write(scope);
}
checkscope();
document.write(scope);

as you would expect results in
local scopelocal scope

Leaving off the var within the function has changed the global
variable rather than setting up a new local variable

So why does the following code not work the same way?

var strReturn1 = "check scope";
function displayNames(){
...
...
strReturn1= "<select>";
...
...
strReturn1=strReturn1 + "</select>";
document.getElementById('col-Names').innerHTML=strReturn1;
}
document.getElementById('col-Names').innerHTML=strReturn1;


document.getElementById('col-Entries').innerHTML=strReturn1;
within the function displays the selection list but the same line
outside the function displays the string "check scope"


But surely strReturn1 the way it is coded here has global scope and
should have been changed by the function????

Using alert or document.write instead of getElementById produce the
same result

I guess I must have a blind spot somewhere.
I would be grateful if someon can identify it for me.

Reg
 
Reply With Quote
 
 
 
 
Elegie
Guest
Posts: n/a
 
      07-05-2011
On 05/07/2011 08:32, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote :

Hi Reg,

> document.getElementById('col-Entries').innerHTML=strReturn1;
> within the function displays the selection list but the same line
> outside the function displays the string "check scope"
>
> But surely strReturn1 the way it is coded here has global scope and
> should have been changed by the function????


You are using a FunctionDeclaration to declare your function. This means
that your function is evaluated and made available at any point of your
script, before the first statement is executed. Would you happen to call
this function before you make the global variable assignment?

If this is not the case, then could you post some test case
demonstrating the issue? As we cannot reproduce the error, it is
difficult to guess what goes wrong.

Also, if you are in good shape, have slept well and got a whole bunch of
coffees already, try and read the following (something I posted recently):

<URL:http://groups.google.com/group/comp.lang.javascript/msg/66e9455bc5edac34>

Regards,
Elegie.

 
Reply With Quote
 
 
 
 
Reg@nospam.com
Guest
Posts: n/a
 
      07-05-2011
On Tue, 05 Jul 2011 10:52:24 +0200, Elegie <(E-Mail Removed)>
wrote:

>On 05/07/2011 08:32, (E-Mail Removed) wrote :
>
>Hi Reg,
>
>> document.getElementById('col-Entries').innerHTML=strReturn1;
>> within the function displays the selection list but the same line
>> outside the function displays the string "check scope"
>>
>> But surely strReturn1 the way it is coded here has global scope and
>> should have been changed by the function????

>
>You are using a FunctionDeclaration to declare your function. This means
>that your function is evaluated and made available at any point of your
>script, before the first statement is executed. Would you happen to call
>this function before you make the global variable assignment?
>
>If this is not the case, then could you post some test case
>demonstrating the issue? As we cannot reproduce the error, it is
>difficult to guess what goes wrong.
>
>Also, if you are in good shape, have slept well and got a whole bunch of
>coffees already, try and read the following (something I posted recently):
>
><URL:http://groups.google.com/group/comp.lang.javascript/msg/66e9455bc5edac34>
>
>Regards,
>Elegie.


Thanks Elegie

I was calling the function from onLoad
Calling it from within the script has fixed the problem.

Regards
Reg
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Having trouble understanding function scope and variable scope Andrew Falanga Javascript 2 11-22-2008 09:23 PM
Scope - do I need two identical classes, each with different scope? ann Java 13 09-13-2005 03:07 AM
How do namespace scope and class scope differ? Steven T. Hatton C++ 9 07-19-2005 06:07 PM
IMPORT STATIC; Why is "import static" file scope? Why not class scope? Paul Opal Java 12 10-10-2004 11:01 PM
How do I scope a variable if the variable name contains a variable? David Filmer Perl Misc 19 05-21-2004 03:55 PM



Advertisments