Actionscript 3 操作脚本3:将字符串变量作为函数调用
我有一个功能Actionscript 3 操作脚本3:将字符串变量作为函数调用,actionscript-3,actionscript,Actionscript 3,Actionscript,我有一个功能 function tempFeedBack():void { trace("called"); } 当我像这样直接写函数名时,事件监听器工作得非常完美 thumbClip.addEventListener(MouseEvent.CLICK, tempFeedBack); thumbClip.addEventListener(MouseEvent.CLICK, this["tempFeedBack"]()); 但是,当我以字符串的形式给出函数名时
function tempFeedBack():void
{
trace("called");
}
当我像这样直接写函数名时,事件监听器工作得非常完美
thumbClip.addEventListener(MouseEvent.CLICK, tempFeedBack);
thumbClip.addEventListener(MouseEvent.CLICK, this["tempFeedBack"]());
但是,当我以字符串的形式给出函数名时
thumbClip.addEventListener(MouseEvent.CLICK, tempFeedBack);
thumbClip.addEventListener(MouseEvent.CLICK, this["tempFeedBack"]());
它不起作用了!它说,TypeError:Error 1006:value不是一个函数
有什么想法吗 之所以出现此错误,是因为[tempFeedBack]不是一个函数对象,它是addEventListener函数的侦听器参数。此外,它什么都不是,因为tempFeedBack函数不能返回任何值 为了更好地理解这一点,您所写的内容相当于:
thumbClip.addEventListener(MouseEvent.CLICK, tempFeedBack());
在这里您可以看到,您已经将tempFeedBack函数的返回值作为侦听器参数传递,该参数应该是函数对象,但是tempFeedBack不能返回任何内容
所以,为了进一步解释,如果你想得到你所写的作品,你应该这样做:
function tempFeedBack():Function
{
return function():void {
trace("called");
}
}
但我认为你的意思是:
// dont forget to set a MouseEvent param here
function tempFeedBack(e:MouseEvent):void
{
trace("called");
}
stage.addEventListener(MouseEvent.CLICK, this["tempFeedBack"]);
// you can also write it
stage.addEventListener(MouseEvent.CLICK, this.tempFeedBack);
希望这能有所帮助