I want to write something like this:
function MyFunction () { .... return SomeString; }
function SomeFunction () { ... return SomeOtherString; }
function SomeCode() {
var TheFunction;
if (SomeCondition) {
TheFunction = SomeFunction;
} else {
TheFunction = SomeCode;
}
var MyVar = TheFunction(); // bugs here
}
Basically, I’d like to execute a function after some condition has been evaluated so that variable MyVar contains contains the result of the function that has been called.
What am I missing to make this work?
Thanks.
If you are just talking about conditionally executing a function and storing the results…
If you want to determine the function to call, but not call it until another time, use:
Callbacks can be very useful in JS…they basically tell the consumer to “call this when you think it’s appropriate”.
Here’s an example of a callback passed to the jQuery
ajax()method.