2011-06-23 140 views
4
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" 

這不起作用,但你能看到我想要做什麼嗎?它有點像函數指針,或者當你將一個函數名傳遞給一個事件監聽器。謝謝。as3功能指針

回答

4

你需要的是:

var plan:Object = { theFunctionName: myFunction } 
16

無論做什麼雅各布建議,或者你甚至可以這樣做:用於設置類型功能

// 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 
+0

+1。爲了保持簡單,人們經常會錯過 – Chris

+0

+1 :) – epologee

+0

+1更好的回答! – W55tKQbuRu28Q4xv