[removed] Passing parameters to a callback function

后端 未结 13 2451
隐瞒了意图╮
隐瞒了意图╮ 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:11

    If you want something slightly more general, you can use the arguments variable like so:

    function tryMe (param1, param2) {
        alert(param1 + " and " + param2);
    }
    
    function callbackTester (callback) {
        callback (arguments[1], arguments[2]);
    }
    
    callbackTester (tryMe, "hello", "goodbye");
    

    But otherwise, your example works fine (arguments[0] can be used in place of callback in the tester)

提交回复
热议问题