0
我們可以將回調函數存儲在對象中,然後通過從對象中檢索來調用回調函數。在對象中存儲回調
var myArray = [];
function myFoo(myCallback)
{
var obj = new Object();
obj.call_back = myCallback; // Store in object
myArray.push(obj); // Add in array
}
function doSomething(results)
{
for(var index=0;index < myArray.length;index++)
{
var obj = myArray[index];
if(obj.hasOwnProperty("call_back"))
{
var callbackMethod = obj.call_back;
callbackMethod(results); // Call callback
}
}
}
是否有效實施?
是的,你可以做到這一點 – Satpal
是。這是可能的 – Beginner
我很確定[這是一個重複](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call)。 – evolutionxbox