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

Discussion in 'Javascript' started by Ray, Oct 30, 2006.

  1. Ray

    Ray Guest

    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
    Ray, Oct 30, 2006
    #1
    1. Advertising

  2. 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_notes/closures.html#clScCh >

    Richard.
    Richard Cornford, Oct 30, 2006
    #2
    1. Advertising

  3. Ray

    RobG Guest

    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
    RobG, Oct 30, 2006
    #3
  4. Ray

    Ray Guest

    Ray, Oct 30, 2006
    #4
  5. Ray

    Ray Guest

    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
    Ray, Oct 30, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Neo Geshel
    Replies:
    3
    Views:
    1,077
    Cor Ligthert
    Jun 10, 2005
  2. Rich
    Replies:
    5
    Views:
    3,947
    Karl Seguin [MVP]
    Feb 22, 2006
  3. Daniel Waite
    Replies:
    2
    Views:
    226
    Daniel Waite
    May 2, 2008
  4. David Filmer
    Replies:
    19
    Views:
    240
    Kevin Collins
    May 21, 2004
  5. Andrew Falanga
    Replies:
    2
    Views:
    201
    Andrew Falanga
    Nov 22, 2008
Loading...

Share This Page