![](/img/trans.png)
[英]Flex/Actionscript - Traceing the contents of a variable of type Function
[英]In ActionScript, is there a way to test for existence of variable with datatype “Function”
所以我有一個類,我實例化一個變量回調,如下所示:
public var callback:Function;
到現在為止還挺好。 現在,我想向此類添加一個事件監聽器並測試是否存在回調。 我這樣做:
this.addEventListener(MouseEvent.MOUSE_OVER, function(event:MouseEvent) : void {
if (callback) {
// do some things
}
});
這很好用,不會拋出任何錯誤,但無論我在哪里測試回調,我都會得到以下警告:
3553: Function value used where type Boolean was expected.
Possibly the parentheses () are missing after this function reference.
這讓我感到煩惱,所以我試圖通過測試null和undefined來擺脫警告。 那些導致錯誤。 我也無法將Function實例化為null。
我知道,我知道,真正的程序員只關心錯誤,而不是警告。 如果這種情況得不到解決,我會活下來。 但它困擾我! :)我只是神經質,或者實際上是否有某種方法來測試是否在沒有IDE婊子的情況下創建了真正的函數?
與使用typeof類似:
if(callback is Function){
}
我相信如果函數存在並且是函數則應該求值為true,如果它是null或不是函數則應該為false。 (雖然如果這不起作用,請嘗試if(callback && callback is function){}
if( !(callback == null)){
// do something
}
已經有一個可行的答案,但我想我會提到你也可以通過顯式地將結果轉換為布爾值來停止警告。
if (Boolean(callback)) {
// do something
}
你有沒有嘗試過:
if (typeof callback == "function") {
// do some things
}
?
http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/operators.html#typeof
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.