How to execute a method passed as parameter to function

前端 未结 8 651
旧巷少年郎
旧巷少年郎 2020-12-13 03:44

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 m

8条回答
  •  不知归路
    2020-12-13 03:52

    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.

提交回复
热议问题