英文:
What's the difference between using a function with "()" and when we use it without them in Javascript?
问题
- 有时候我们用括号调用函数,有时候我们不用。
比如在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;
- 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论