472,129 Members | 1,702 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,129 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 2535
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Chris | last post: by
6 posts views Thread by Maarten | last post: by
5 posts views Thread by Jeff | last post: by
pbmods
1 post views Thread by pbmods | last post: by
4 posts views Thread by subramanian100in | last post: by
7 posts views Thread by David Mathog | last post: by

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.