开发者

What's wrong with putting () at the end when calling a function? [duplicate]

开发者 https://www.devze.com 2023-04-05 07:24 出处:网络
This开发者_运维技巧 question already has answers here: Closed 11 years ago. Possible Duplicate: In JavaScript, does it make a difference if I call a function with parentheses?
This开发者_运维技巧 question already has answers here: Closed 11 years ago.

Possible Duplicate:

In JavaScript, does it make a difference if I call a function with parentheses?

This may sound very basic, but I really don't understand.

I try to take () off from an event handler and it cause a bug in my JavaScript webpage, what is the difference between putting and not putting () at the end when calling a function.

My example is:

document.getElementById("searchField").onkeyup = searchSuggest;

when I changed to searchSuggest(), my function was not working.

Can anyone please explain to me? If I make any part unclear, please ask, thank you.


When you write:

document.getElementById("searchField").onkeyup = searchSuggest();

You're calling the searchSuggest function and assigning the value it returns to the onkeyup member. In your case, you probably want to assign the function itself to onkeyup, so you should write:

document.getElementById("searchField").onkeyup = searchSuggest;
0

精彩评论

暂无评论...
验证码 换一张
取 消