13Feb AS3 Best Practice for Callback Evals
1,891 views
UPDATED: Thanks to my readers- there is an even easier way to evaluate a function as being not null without a compiler warning. One suggested a method using a type check (functionName is Function) however this is process intensive task for the compiler especially if this has to be done within a game loop. I believe the most efficient solution was suggested by Mije for doing a strict comparison by !== against null. This is better than != as the previous evaluation executes faster.
One of the techniques I am fond of is callback functions when using a custom event may be overkill for the situation. However, trying to actually explain callbacks verbally is almost impossible. In a nutshell, it’s a method (lets say A) that accepts another a method (lets say B) as a property. Method A will perform its action and then call method B. This allows the class that uses Method A to have one of its methods called once Method A completed.
Here is an example; however, in practice, callbacks are only useful when the method that uses it is in a different class otherwise you could just call the method directly instead of using the callback.
private function gameEnded():void {
saveScore(10, onSave);
}
private function saveScore(score:int, onSaveCallback:Function):void {
// saving score...
// once done:
onSaveCallback();
}
// This is the function that gets past into saveScore
private function onSave():void {}
Okay okay, now here is the problem at hand. What if the callback function was optional? You could write it like this:
Not so fast! If the callback function is not specified and gets set to null, Flash will throw an error since you cannot call null as a function like this: null().
Well, you could do a conditional check:
This way we can check to see if onSaveCallback does not equal null. Well, this angers the Flash compiler and it will throw the below message:
Warning: 3553: Function value used where type Boolean was expected. Possibly the parentheses () are missing after this function reference.
This is because Flash is confused that you are trying to evaluate a function as a Boolean instead of using it normally as a function. So, how can we evaluate if the function is not null without throwing a Flash warning? Well, I scowered the internet and could not find a best practice for Flash. After a discussion with my readers, the best solution here is to do a strict type check to null as shown below:
Well there you have it- let me know if this works for you!
Auto-Generated Related Posts:

February 14th, 2009 at 12:09 am
There’s actually an easier solution-
Instead of:
if(onSaveCallback) onSaveCallback();
Do this:
if(onSaveCallback != null) onSaveCallback();
February 14th, 2009 at 1:09 am
I believe the following also works:
if( functionArg is Function )
February 14th, 2009 at 1:33 am
Hm. Why not do this simple check?
if(onSaveCallback !== null) onSaveCallback();
February 14th, 2009 at 5:17 am
How about using ‘is’:
if(onSaveCallback is Function) onSaveCallback();
Can’t help thinking a custom event is even cleaner…
February 14th, 2009 at 6:06 am
Why not just do this test :
if(onSaveCallback != null) onSaveCallback();
February 14th, 2009 at 7:42 am
Hello,
First sorry for my bad English…
My 2 cents, it seems a bit overkill just to test nullity of a function, since the short way make flash compiler complained, try the explicit way:
function foo(bar:Function=null):void{
if (bar!=null)
bar();
}
It will be faster than cast to an object and then compare to a string.
Regards.
February 14th, 2009 at 9:08 am
The most common way to handle this type of comparison:
…just a little easier.
February 27th, 2009 at 5:37 pm
Callbacks are fine but sort of the old way to do things… You should really use event listeners as that supports multiple “call backs” for a specific object.