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

前端 未结 6 2448
别跟我提以往
别跟我提以往 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();
    

提交回复
热议问题