有什么方法可以将函数作为字符串调用?
Any way to Call A Function As A String?
我刚刚开始一个项目,想知道是否可以通过字符串调用函数(在事件监听器中)。
import flash.utils.Timer;
import flash.events.Event;
import flash.events.TimerEvent;
var threesec:Timer=new Timer(3000, 1);
var whaton:String="tsecc"
threesec.start();
threesec.addEventListener(TimerEvent.TIMER_COMPLETE, whaton);
function tsecc(tsecc:TimerEvent):void{
trace("Hello")
threesec.reset();
threesec.start();
}
由于这一行,这不起作用:
threesec.addEventListener(TimerEvent.TIMER_COMPLETE, whaton);
和此错误代码:
1067: Implicit coercion of a value of type String to an unrelated type Function.
我知道我的做法大错特错,但有没有正确的方法来调用字符串格式的函数?
我是否必须向变量添加一个 属性,我是否必须创建另一种类型的变量?
threesec.addEventListener(TimerEvent.TIMER_COMPLETE, this[whaton]);
我用 "bracket syntax" 来做到这一点。您可以在网上搜索了解更多信息。
我刚刚开始一个项目,想知道是否可以通过字符串调用函数(在事件监听器中)。
import flash.utils.Timer;
import flash.events.Event;
import flash.events.TimerEvent;
var threesec:Timer=new Timer(3000, 1);
var whaton:String="tsecc"
threesec.start();
threesec.addEventListener(TimerEvent.TIMER_COMPLETE, whaton);
function tsecc(tsecc:TimerEvent):void{
trace("Hello")
threesec.reset();
threesec.start();
}
由于这一行,这不起作用:
threesec.addEventListener(TimerEvent.TIMER_COMPLETE, whaton);
和此错误代码:
1067: Implicit coercion of a value of type String to an unrelated type Function.
我知道我的做法大错特错,但有没有正确的方法来调用字符串格式的函数?
我是否必须向变量添加一个 属性,我是否必须创建另一种类型的变量?
threesec.addEventListener(TimerEvent.TIMER_COMPLETE, this[whaton]);
我用 "bracket syntax" 来做到这一点。您可以在网上搜索了解更多信息。