How to execute a method passed as parameter to function

前端 未结 8 659
旧巷少年郎
旧巷少年郎 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 04:18

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

提交回复
热议问题