as3 function pointer

百般思念 提交于 2019-12-19 08:28:48

问题


private function myFunction(numIn:Number){
   trace ("numIn " + numIn);
}

var plan:Object = { "theFunctionName": "myFunction" }


// now use the function 

plan.theFunctionName(5);

// should trace out:  "numIn 5"

This won't work, but can you see what I'm trying to do? It's kind of like a function pointer, or when you pass a function name into an event listener. Thanks.


回答1:


What you need is:

var plan:Object = { theFunctionName: myFunction }



回答2:


Either do what Jacob suggested, or you can even just do:

// Your function.
function myFunction(numIn:Number):void
{
    trace("numIn " + numIn);
}


// Assign "myFunction" to the property "callback" of type "Function".
var callback:Function = myFunction;

// Call "myFunction" via "callback".
callback(5); // numIn 5


来源:https://stackoverflow.com/questions/6461591/as3-function-pointer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!