scope of local variables across async method calls with anon functions

J

John Davison

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
 
R

Richard Cornford

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:-

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.
 

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,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top