Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Javascript (http://www.velocityreviews.com/forums/f68-javascript.html)
-   -   Variable Scope inside a function--Did I get this correctly? (http://www.velocityreviews.com/forums/t927851-variable-scope-inside-a-function-did-i-get-this-correctly.html)

Ray 10-30-2006 09:58 AM

Variable Scope inside a function--Did I get this correctly?
 
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


Richard Cornford 10-30-2006 01:25 PM

Re: Variable Scope inside a function--Did I get this correctly?
 
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.


RobG 10-30-2006 01:40 PM

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

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


Ray 10-30-2006 02:04 PM

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

Richard Cornford wrote:
<snip>
> Nested functions.
>
> <URL: http://www.jibbering.com/faq/faq_not...es.html#clScCh >
>
> Richard.


Thanks, Richard! Much appreciated.


Ray 10-30-2006 02:05 PM

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

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



All times are GMT. The time now is 05:58 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.