What's the difference between using a function with "()" and when we use it without them in Javascript?

huangapple go评论113阅读模式
英文:

What's the difference between using a function with "()" and when we use it without them in Javascript?

问题

  1. 有时候我们用括号调用函数,有时候我们不用。

比如在DOM中,我明白为什么在事件监听器中不使用括号,因为我们希望它们只在发生点击时起作用。但在Node中,就像上面的例子一样,为什么我们没有使用任何括号?

英文:
function find() {
    console.log("Request handler 'find' was called.");
}
function display() {
    console.log("Request handler 'display' was called.");
}

exports.find = find;
exports.display = display;

  1. sometime we call function with parentheses and sometimes we don't use them.

Like in DOM i get why we do no t use it with event Listeners because we want to them to only work when click occurs, But in node like the above example why we didn't used any parentheses?

答案1

得分: 4

不带括号时,您正在分配函数本身(函数是一级对象),而带括号时,您正在执行该函数并分配结果值。

英文:

without the parentheses you are assigning the function itself ( functions are first class objects ) whereas with the parentheses you are executing the function and assigning the resultant value

huangapple
  • 本文由 发表于 2023年8月4日 23:55:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76837490.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定