ps0604 ps0604 - 5 months ago 11
Javascript Question

Call function received as a parameter in Javascript

I have the following code with a function:

var f1;

var getFunction = function (f2) {
f1 = f2;
}


Later in the code, I need to call
f1
(that was actually received as a parameter). Note that I don't know the function's name when I call it.

How do I make this call?

Answer
function test(){
     alert(1);
}
var f1;

var getFunction = function (f2) {
    f1 = f2; 
    f1();  
}

getFunction(test);

Here is the DEMO