473,287 Members | 1,426 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,287 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 2663
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): ----------------------------------------------------------...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

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.