How to execute a method passed as parameter to function

懵懂的女人 提交于 2019-11-28 19:04:18

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.

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.

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

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

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.

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'});

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