469,275 Members | 1,543 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,275 developers. It's quick & easy.

scope of local variables across async method calls with anon functions

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
Mar 6 '07 #1
1 1510
On Mar 6, 5:13 pm, John Davison <unixfreak0...@gmail.comwrote:
<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.
Mar 6 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Andy Baker | last post: by
6 posts views Thread by pembed2003 | last post: by
4 posts views Thread by Marc Tanner | last post: by
3 posts views Thread by Grant Wagner | last post: by
2 posts views Thread by JJ | last post: by
5 posts views Thread by Sandra-24 | last post: by
8 posts views Thread by dd | last post: by
pbmods
1 post views Thread by pbmods | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.