How to execute a method passed as parameter to fun

2019-01-22 10:57发布

问题:

I want to write my own function in JavaScript which takes a callback method as a parameter and executes it after the completion, I don't know how to invoke a method in my method which is passed as an argument. Like Reflection.

example code

function myfunction(param1, callbackfunction)
{
    //do processing here
    //how to invoke callbackfunction at this point?
}

//this is the function call to myfunction
myfunction("hello", function(){
   //call back method implementation here
});

回答1:

You can just call it as a normal function:

function myfunction(param1, callbackfunction)
{
    //do processing here
    callbackfunction();
}

The only extra thing is to mention context. If you want to be able to use the this keyword within your callback, you'll have to assign it. This is frequently desirable behaviour. For instance:

function myfunction(param1, callbackfunction)
{
    //do processing here
    callbackfunction.call(param1);
}

In the callback, you can now access param1 as this. See Function.call.



回答2:

I too came into same scenario where I have to call the function sent as parameter to another function.

I Tried

mainfunction('callThisFunction');

First Approach

function mainFuntion(functionName)
{
    functionName();
}

But ends up in errors. So I tried

Second Approach

functionName.call(). 

Still no use. So I tried

Third Approach

this[functionName]();

which worked like a champ. So This is to just add one more way of calling. May be there may be problem with my First and Second approaches, but instead googling more and spending time I went for Third Approach.



回答3:

function myfunction(param1, callbackfunction)
{
    //do processing here
   callbackfunction(); // or if you want scoped call, callbackfunction.call(scope)
}


回答4:

object[functionName]();

object: refers to the name of the object.

functionName: is a variable whose value we will use to call a function.

by putting the variable used to refer to the function name inside the [] and the () outside the bracket we can dynamically call the object's function using the variable. Dot notation does not work because it thinks that 'functionName' is the actual name of the function and not the value that 'functionName' holds. This drove me crazy for a little bit, until I came across this site. I am glad stackoverflow.com exists <3



回答5:

Another way is to declare your function as anonymous function and save it in a variable:

var aFunction = function () {
};

After that you can pass aFunction as argument myfunction and call it normally.

function myfunction(callbackfunction) {
    callbackfunction();
}

myfunction(aFunction);

However, as other answers have pointed out, is not necessary, since you can directly use the function name. I will keep the answer as is, because of the discussion that follows in the comments.



回答6:

I will do something like this

var callbackfunction = function(param1, param2){
console.log(param1 + ' ' + param2)
}

myfunction = function(_function, _params){
_function(_params['firstParam'], _params['secondParam']);
}

Into the main code block, It is possible pass parameters

myfunction(callbackfunction, {firstParam: 'hello', secondParam: 'good bye'});


回答7:

All the examples here seem to show how to declare it, but not how to use it. I think that's also why @Kiran had so many issues.

The trick is to declare the function which uses a callback:

function doThisFirst(someParameter,  myCallbackFunction) {
    // Do stuff first
    alert('Doing stuff...');

    // Now call the function passed in
    myCallbackFunction(someParameter);
}

The someParameter bit can be omitted if not required.

You can then use the callback as follows:

doThisFirst(1, myOtherFunction1);
doThisFirst(2, myOtherFunction2);

function myOtherFunction1(inputParam) {
    alert('myOtherFunction1: ' + inputParam);
}

function myOtherFunction2(inputParam) {
    alert('myOtherFunction2: ' + inputParam);
}

Note how the callback function is passed in and declared without quotes or brackets.

  • If you use doThisFirst(1, 'myOtherFunction1'); it will fail.
  • If you use doThisFirst(1, myOtherFunction3()); (I know there's no parameter input in this case) then it will call myOtherFunction3 first so you get unintended side effects.