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

R

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
 
R

Richard Cornford

Ray said:
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_notes/closures.html#clScCh >

Richard.
 
R

RobG

Ray said:
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.
;-)
 
R

Ray

RobG said:
Ray said:
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.
;-)

Thanks Rob! :)

Ray
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top