Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In ActionScript, is there a way to test for existence of variable with datatype "Function"

So I have a class where I instantiate a variable callback like so:

public var callback:Function;

So far so good. Now, I want to add an event listener to this class and test for existence of the callback. I'm doing like so:

this.addEventListener(MouseEvent.MOUSE_OVER, function(event:MouseEvent) : void {
        if (callback) {
            // do some things
        }
});

This works great, doesn't throw any errors, but everywhere I test for callback I get the following warning:

3553: Function value used where type Boolean was expected.  
Possibly the parentheses () are missing after this function reference.

That bugged me, so I tried to get rid of the warning by testing for null and undefined. Those caused errors. I can't instantiate a Function as null, either.

I know, I know, real programmers only care about errors, not warnings. I will survive if this situation is not resolved. But it bothers me! :) Am I just being neurotic, or is there actually some way to test whether a real Function has been created without the IDE bitching about it?

like image 529
Robusto Avatar asked Jan 22 '23 03:01

Robusto


2 Answers

Similar to using typeof:

if(callback is Function){

}

I believe should evaluate to true if the function exists and is a function and false if it is null or is not a function. (although if that doesn't work try if(callback && callback is function){}

like image 145
quoo Avatar answered Jan 24 '23 15:01

quoo


if( !(callback == null)){
 // do something
}
like image 45
JeffryHouser Avatar answered Jan 24 '23 15:01

JeffryHouser