473,508 Members | 2,382 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Variable Scope inside a function--Did I get this correctly?

Ray
Hello,

I think I've had JavaScript variable scope figured out, can you please
see if I've got it correctly?

* Variables can be local or global
* When a variable is declared outside any function, it is global
regardless of whether it's declared with or without "var"
* When it is declared inside a function, if declared with "var", it's
local, if not, it's global
* A local variable that is declared inside a function is local to the
whole function, regardless of where it is declared, e.g.:

function blah() {
for(var i ... ) {
var j ...
}

}

i and j will both be visible within blah() after their declaration.
* the notion of "function" in this context also applies for this kind
of construct:

var myHandler =
{
onClickDo: function()
{

in the sense that whatever one declares inside onClickDo with "var"
will only be visible inside onClickDo.

What else, am I missing anything?

Thanks for any pointers/corrections,
Ray

Oct 30 '06 #1
4 2681
Ray wrote:
I think I've had JavaScript variable scope figured out, can you
please see if I've got it correctly?

* Variables can be local or global
Where 'local' means local to a function, but functions may be nested
and a variable local to a function that contains another may be visible
inside the contained function if not 'masked' by an identically named
local variable, formal parameter or inner function declaration within
the contained function.
* When a variable is declared outside any function, it is global
regardless of whether it's declared with or without "var"
If - var - is not used then a variable is not declared at all. You are
describing an assignment to an (undeclared) Identifier. The effect of
an assignment to an undeclared Identifier is the creation of a property
of the global object with a name corresponding with the Identifier, and
declaring a global variable also results in the creation of a property
of the global object, so the two seem similar. However, a variable
declaration results in the creation of a property on the pertinent
object (the global object in the case of a global variable) as code
enters the relevant execution context, while assigning to an undeclared
Identifier does not create a property of the global object until the
assignment. Also, the property resulting from a variable declaration
cannot be deleted, while a property resulting form assignment can be.
* When it is declared inside a function, if declared with "var", it's
local, if not, it's global
Without -var - the assignment to an undeclared Identifier results in
the creation of a property of the global object wherever it occurs (at
least without objects with property names corresponding with the
Identifier having been explicitly added to the scope chain with the -
with - statement).
* A local variable that is declared inside a function is local to the
whole function, regardless of where it is declared, e.g.:
Yes.
function blah() {
for(var i ... ) {
var j ...
}

}

i and j will both be visible within blah()
Yes.
after their declaration.
They are also visible before their declaration. All the declarations
for a function are processed prior to the execution of any function
body code.
* the notion of "function" in this context also applies for this kind
of construct:

var myHandler =
{
onClickDo: function()
{

in the sense that whatever one declares inside onClickDo with "var"
will only be visible inside onClickDo.

What else, am I missing anything?
Nested functions.

<URL: http://www.jibbering.com/faq/faq_not...es.html#clScCh >

Richard.

Oct 30 '06 #2

Ray wrote:
Hello,

I think I've had JavaScript variable scope figured out, can you please
see if I've got it correctly?
[...]
What else, am I missing anything?
In addition to Richard's response (I think you could write several
pages on "what else" :-) )

1. Formal parameters are also local variables of function objects:

function fred( param0, param1, ...){ ... }

The formal parameters are created as local variables.
2. Local variables can mask global variables (or those belonging to
'outer' scopes) if they have the same name:

var a;
function b() {
var a;
/* b can only see local a, needs windw.a to see global a */
}

That's more a consequence of scope, but is a "what else" in my book.
;-)
--
Rob

Oct 30 '06 #3
Ray

Richard Cornford wrote:
<snip>
Nested functions.

<URL: http://www.jibbering.com/faq/faq_not...es.html#clScCh >

Richard.
Thanks, Richard! Much appreciated.

Oct 30 '06 #4
Ray

RobG wrote:
Ray wrote:
Hello,

I think I've had JavaScript variable scope figured out, can you please
see if I've got it correctly?
[...]
What else, am I missing anything?

In addition to Richard's response (I think you could write several
pages on "what else" :-) )

1. Formal parameters are also local variables of function objects:

function fred( param0, param1, ...){ ... }

The formal parameters are created as local variables.
2. Local variables can mask global variables (or those belonging to
'outer' scopes) if they have the same name:

var a;
function b() {
var a;
/* b can only see local a, needs windw.a to see global a */
}

That's more a consequence of scope, but is a "what else" in my book.
;-)
--
Rob
Thanks Rob! :)

Ray

Oct 30 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1930
by: Chris | last post by:
Hi, I'm having trouble with variable scope inside procedures. this is pseudocode LDAP NewUser; if (something = true) { NewUser = LDAP.FindUser(use emailaddress)
6
1457
by: Maarten | last post by:
Here is a nice problem. I have two scripts, one calling the other with via an include. The script that is included contains a variable, a function and a call to that function. The variable needs to...
0
1338
by: EADeveloper | last post by:
Quick ASP.Net architectural question.... We're creating a set of standard web controls for use in our app, and those custom controls need access to a set of variables that we are initializing at...
2
4097
by: Kevin Walzer | last post by:
I'm trying to construct a simple Tkinter GUI and I'm having trouble with getting the value of an entry widget and passing it onto a callback function. But I'm not grokking variable scope correctly....
5
4785
by: Jeff | last post by:
Hey Below is a C# program I've made.... it's an app where I experiment with variable scope. In this code you see two variables named i (one is a local variable and the other is a member of the...
1
25621
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause...
0
35189
MMcCarthy
by: MMcCarthy | last post by:
We often get questions on this site that refer to the scope of variables and where and how they are declared. This tutorial is intended to cover the basics of variable scope in VBA for MS Access. For...
4
7050
by: subramanian100in | last post by:
Consider the program #include <iostream> using namespace std; class Test { public: Test(Test_int c_value)
7
1876
by: David Mathog | last post by:
I accidentally did this the other day (it was a lot less obvious in the much longer actual program, hundreds of lines are omitted): ----------------------------------------------------------...
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7115
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7321
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7489
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5047
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1547
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
414
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.