tags:

views:

43

answers:

5

I'm still trying to understand javascript scoping. What is the simplest way to return a value from an anonymous callback function? For instance,

var test = 'outside';
callsFunction(function() { test = 'inside'; });
console.log(test);

where callsFunction calls the anonymous function. I would like this to print 'inside'.

A: 

Did you call callsFunction?

Eamorr
+2  A: 

I'm a little bit confused, but I believe this is what you're after

function callsFunction( fn )
{
  fn();
}

var test = 'outside';
callsFunction(function() { test = 'inside'; });
console.log(test);

Note that any function can be invoked via the usage of (). However, there are special methods for invoking functions from different contexts, such as Function.call() and Function.apply()

Peter Bailey
Ok, this is how I thought things work, but it's not working for me where callsFunction is an ajax call and my anonymous function is the success callback. Maybe I have a different problem?
Tristan
Working with callbacks as part of an AJAX process is different because of the asynchronous nature of AJAX. If you're having an AJAX issue you need to post the specifics of that.
Peter Bailey
Yes, that's the problem. I'm getting to console.log before the callback is actually called.
Tristan
I don't see how this solution solved your problem. I think your problem is that callsFunction is asynchronous (calling ajax), you should follow Casey Hope's advice and add your call to the callback. More details about the real problem would help others see what the real problem is. I added a comment there too
Juan Mendes
+2  A: 

At the risk of pointing out the obvious, the simplest way to have something done after a callback is to simply include it in the callback (and this is, in fact, the way to go):

var test = 'outside';
callsFunction(function() { 
    test = 'inside'; 
    console.log(test);
});

A real example: let's say you want to update a div with the contents of a file from an Ajax call.

Ajax('file.html', function (data) {
    // Update the div here
    document.getElementById('d').innerHTML = data;
});

// Not here
Casey Hope
Thank, but I do indend to use it outside the callback context.
Tristan
How do you intend to use it? Maybe post some actual code?
Casey Hope
You should just create a separate method to do the common work and call it from the callback and wherever else you need it.
Juan Mendes
+1  A: 

Maybe what you are wanting to research is closures in JavaScript?

Anthony -GISCOE-
A: 

You didn't define callsFunction in your question, but if you make it a self-invoking anonymous function, you can return the new 'test' value:

var test = 'outside';
(function() { test = 'inside'; })();

console.log(test); // returns 'inside'

If you do define callsFunction:

var test = 'outside';
var callsFunction = function() { 
    test = 'inside';
    return test;
};

console.log(callsFunction()); // returns 'inside'

You can get more complicated with closures.

Steve Hansell