In ActionScript, is there a way to check for a variable with a data type of "Function",

So, I have a class in which I create a callback variable like this:

public var callback:Function; 

So far so good. Now I want to add an event listener to this class and check for a callback. I do like this:

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

This works fine, does not cause any errors, but wherever I test the callback, I get the following warning:

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

This listened to me, so I tried to get rid of the warning by checking it for null and undefined. This caused errors. I also cannot create an instance of the function as null.

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! :) I'm just a neurotic, or is there really some way to check if a real function was created without the IDE cheating on it?

+4
source share
4 answers

Similar to using typeof:

 if(callback is Function){ } 

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

+6
source
 if( !(callback == null)){ // do something } 
+2
source

There is already an answer that works, but I thought I could mention that you can also stop this warning by explicitly passing the result to Boolean.

 if (Boolean(callback)) { // do something } 
+1
source

You tried:

 if (typeof callback == "function") { // do some things } 

?

http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/operators.html#typeof

0
source

Source: https://habr.com/ru/post/1309143/


All Articles