What is the difference between a function call and function reference?

前端 未结 6 2437
别跟我提以往
别跟我提以往 2020-11-22 05:50

I have the following function

function hello() {
 alert(\"hi!\");
}

Take this piece of code:

var elem = document.getElem         


        
6条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-22 05:55

    Do you want it to execute NOW? Then call it.

    a=hello() means "Call hello() ASAP, and set its return value to a".

    On the other hand, a=hello means "a is an alias for hello. If you call a(), you get the same results as calling hello()"

    You use the latter for callbacks, etc, where you want to tell the browser what you want to happen after an event occurs. For example, you may want to say "call hello() when the user clicks" (as in the example). Or, "When the AJAX query returns a result, call the callback() function on the returned data".

提交回复
热议问题