Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

local & global vars 1

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
GB
hi, possibly a stupid question coming up..

If I define a global var at the top of a script..

Code:
var myGlobal = 'username';

then later on call a func which uses the same var for its input..
Code:
function myFunc(myGlobal){do whatever;}

is the function using a local var or the global var?

in PERL local scope is relative to where within the curly braces the variable resides, so that would make it the global var not a local var.

is it the same with JS?

or because it is defined within the function does it always make them local vars?




"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
Hi

It is local, because the formal parameters are local.

As far as I know, this is true for all languages which uses formal parameters. Personally I can not compare it with [tt]perl[/tt], which uses no formal parameters.

Feherke.
 
I thought so , but just wanted to check before I trashed global vars using the same names for formal parameters

Personally I can not compare it with perl, which uses no formal parameters.

I wouldn't say that , what is the @_ collection then? Which is where all vars passed to a sub are stored.

I guess you could still trash vars passed to a sub if you did say
Code:
@_ = split(/\,/,"a,b,c,d,e,f");

But i'm going off on a tangent , this is a JS forum.

Thanks for the help. :)

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
Hi

1DMF said:
what is the @_ collection then?
A special variable.

I know, [tt]perl[/tt] documentation explicitly states that it is a local array, but because the way its values are assigned I still would not compare it to formal parameters.

( Here I am thinking to this special case : )
perldoc.perl.org said:
Code:
[teal]&[/teal]foo[teal];[/teal] [gray]# foo() get current args, like foo(@_) !![/gray]

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top