[removed] Passing parameters to a callback function

后端 未结 13 2475
隐瞒了意图╮
隐瞒了意图╮ 2020-11-22 15:48

I\'m trying to pass some parameter to a function used as callback, how can I do that?

function tryMe (param1, param2) {
    alert (param1 + \" and \" + param         


        
13条回答
  •  天命终不由人
    2020-11-22 16:19

    This would also work:

    // callback function
    function tryMe (param1, param2) { 
        alert (param1 + " and " + param2); 
    } 
    
    // callback executer 
    function callbackTester (callback) { 
        callback(); 
    } 
    
    // test function
    callbackTester (function() {
        tryMe("hello", "goodbye"); 
    }); 
    

    Another Scenario :

    // callback function
    function tryMe (param1, param2, param3) { 
        alert (param1 + " and " + param2 + " " + param3); 
    } 
    
    // callback executer 
    function callbackTester (callback) { 
    //this is the more obivous scenario as we use callback function
    //only when we have some missing value
    //get this data from ajax or compute
    var extraParam = "this data was missing" ;
    
    //call the callback when we have the data
        callback(extraParam); 
    } 
    
    // test function
    callbackTester (function(k) {
        tryMe("hello", "goodbye", k); 
    }); 
    

提交回复
热议问题