简体   繁体   中英

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. What is getJSON if it is not a callback function, when I don't wrap it inside an arrow 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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