473,385 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 2670
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
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
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
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
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
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
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
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
by: subramanian100in | last post by:
Consider the program #include <iostream> using namespace std; class Test { public: Test(Test_int c_value)
7
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): ----------------------------------------------------------...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.