Calling vs invoking a function

后端 未结 5 1669
北恋
北恋 2020-12-24 05:45

Up to this point, I thought \"calling\" and \"invoking\" a function meant the same thing. However, in a YouTube tutorial it said to invoke a function by calling it.

5条回答
  •  有刺的猬
    2020-12-24 06:24

    The difference is semantic and subtle. When you call a function, you are directly telling it to run. When you invoke a function, you are letting something run it.

    There is one way to call a function:

    myFunction()
    

    Here, you are invoking the function (letting it run) by calling it directly.

    There are many ways to invoke a function (given throughout different comments and answers). Here's one example:

    function invoker(functionName) {
      functionName() 
    }
    
    invoker(myFunction)
    

    Here, by calling invoker, you are invoking myFunction, which is being called indirectly.

提交回复
热议问题