Calling vs invoking a function

后端 未结 5 1692
北恋
北恋 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:09

    Many people use the term calling and invoking interchangeably but that's not right. There is a very slight difference between calling and invoking a function. In JavaScript functions can be invoked without being called which means that the code inside the body of the function can be executed without creating an object for the same. It is tied to the global object. When there is no individual object, the value of this is associated with the global object.

    There is also a difference between call and apply, the fundamental difference is that call() accepts an argument list, while apply() accepts a single array of arguments. A different this object can be assigned when calling an existing function. this refers to the current object, the calling object. With call, you can write a method once and then inherit it in another object, without having to rewrite the method for the new object.

    So, the major difference between invoking and calling comes in terms of the this object. Calling let's you set the this value whereas invoking just ties it to the global object.

提交回复
热议问题