简体   繁体   English

在回调中调用 function 与调用 function

[英]Calling a function vs calling a function in a callback

I want to understand how the getJSON function is invoked differently in the following two situations.我想了解在以下两种情况下如何以不同方式调用getJSON function。 What is getJSON if it is not a callback function, when I don't wrap it inside an arrow function?如果getJSON不是回调 function,当我不将它包装在箭头 function 中时,它是什么?

First:第一的:

btn.addEventListener("click", ()=> {
  getJSON(astroURL);
})

Second:第二:

btn.addEventListener("click", getJSON(astroURL))

The second argument to addEventListener is supposed to be the callback that is invoked each time the event occurs. addEventListener的第二个参数应该是每次事件发生时调用的回调。 Your first example is passing an arrow function as the callback, while your second example is passing the return value of getJSON(astroURL) to addEventListener , which is erroneous, unless the method itself returns a function.您的第一个示例将箭头 function 作为回调传递,而您的第二个示例将getJSON(astroURL)的返回值传递给addEventListener ,这是错误的,除非方法本身返回 function。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM