How exactly callbacks get their arguments?

前端 未结 1 1453
慢半拍i
慢半拍i 2021-01-28 16:34

I have trouble understanding how callbacks in JavaScript get their arguments. Or in other words: how to implement a higher-order function so its callback accepts for instance a

相关标签:
1条回答
  • 2021-01-28 16:53

    Arguments are passed to a function when that function is called.

    function foo(arg) {
        console.log(arg);
    }
    
    foo("This is the value");

    This is still true when it is a callback function.

    function foo(arg) {
        console.log(arg);
    }
    
    function bar(callback) {
        callback("This is the value");
    }
    
    bar(foo);

    And it is still true when the callback function is called by code written by someone else that exists in a library you aren't examining the source code of.

    0 讨论(0)
提交回复
热议问题