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

前端 未结 6 2352
别跟我提以往
别跟我提以往 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:53

    Pretty much all statements in JavaScript have a return value. Unless otherwise specified, functions in JavaScript will return undefined when called. So, the only context in which it would make sense to call your function in this assignment statement if it were to return a function:

    function hello() {
        return function() {
            alert("hi!");
        }
    }
    
    elem.onclick = hello();
    
    0 讨论(0)
  • 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".

    0 讨论(0)
  • 2020-11-22 06:06

    How can I know where I'm supposed to give a reference to the function, and when I'm supposed to actually call it?

    Do you need the function to run now?

    Than add the () to execute it

    Do you need to function to be referenced so it is called later?

    Do not add the ().

    0 讨论(0)
  • 2020-11-22 06:15

    hello() means you call that function, which means the function will be executed directly.

    while when you have elem.onclick = hello, this is called a callback. Where hello doesn't get executed directly but only when a certain event is fired (in this case when there's a click on the element)

    0 讨论(0)
  • 2020-11-22 06:16

    Well, the onclick property expects a reference to a function, for it to execute when the element is clicked. Normally it's either:

    element.onclick = funcRef;
    

    or

    element.onclick = function () {
        funcRef();
    };
    

    (but of course, it's best to use addEventListener and attachEvent)

    Notice how both of them are references to functions, not calling.

    When something expects a reference, you don't call it...you assign a reference to it (first example).

    When you want to specifically call a function, you call it with () (second example). But notice how in the second example, there's still a reference to a function that's assigned to onclick - it's just an anonymous function.

    Probably the more important part:

    Some people think you want to do this:

    element.onclick = funcRef();
    

    But that immediately executes the function (because of the ()), and assigns its return value to onclick. Unless the return value is a function, this isn't what you want.

    I think the moral of the story is that when you want/need something to execute right now, you call the function. If the function is wanted for later use or needs stored, you don't call it.

    0 讨论(0)
  • 2020-11-22 06:16

    A reference to your function is needed somewhere no matter how it gets called. The difference here is that you are not explicitly calling the hello function. You are assigning a reference to that function to the elem DOM node's onclick event handler so that when onclick is fired for that Node, your function gets called.

    0 讨论(0)
提交回复
热议问题