scope of local variables across async method calls with anon functions

Discussion in 'Javascript' started by John Davison, Mar 6, 2007.

  1. John Davison

    John Davison Guest

    When you have the following:

    function foo() {
    var bar = 10;
    // do some other stuff
    }

    It is my understanding that bar is *not* a property of function object
    foo. Now, when you have the following type of code

    function foo() {
    var bar = some_user_input();
    var f = function() {
    // do something with bar
    };

    // set f to get called by some async method
    }

    when f() runs bar will be in scope. With an asyncronous method, f() will
    get called some time later. Assume that foo() is called a first time, and
    f() is set to execute eventually. What happens to bar if foo() gets
    called before the first f() executes? Does the first foo() have bar on
    its stack? Or do the two calls to foo() have the same stack?

    I guess my question is: in that context is bar treated like a property or
    a local variable?

    John
    John Davison, Mar 6, 2007
    #1
    1. Advertising

  2. On Mar 6, 5:13 pm, John Davison <> wrote:
    <snip>
    > when f() runs bar will be in scope. With an asyncronous method, f() will
    > get called some time later. Assume that foo() is called a first time, and
    > f() is set to execute eventually. What happens to bar if foo() gets
    > called before the first f() executes? Does the first foo() have bar on
    > its stack? Or do the two calls to foo() have the same stack?


    See:-

    <URL: http://jibbering.com/faq/faq_notes/closures.html >

    > I guess my question is: in that context is bar treated like a
    > property or a local variable?


    Local variables are properties of the Variable object for each
    execution context, so both.

    Richard.
    Richard Cornford, Mar 6, 2007
    #2
    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. john
    Replies:
    0
    Views:
    428
  2. andy6
    Replies:
    2
    Views:
    756
    andy6 via DotNetMonster.com
    Jun 9, 2006
  3. Dave
    Replies:
    2
    Views:
    525
    Chris Rebert
    May 15, 2010
  4. Steven
    Replies:
    0
    Views:
    358
    Steven
    Nov 30, 2005
  5. Jang
    Replies:
    4
    Views:
    117
    -Lost
    Jun 18, 2007
Loading...

Share This Page