Why do I have to omit parentheses when passing a function as an argument?

前端 未结 2 1066
醉梦人生
醉梦人生 2020-12-15 12:44

I am trying to wrap my head around as to why the following code results in a stack overflow when the parentheses are included, but do not when they omitted.

I am ca

2条回答
  •  情歌与酒
    2020-12-15 13:10

    By writing

    foo()
    

    you're actually calling foo at that moment. Which of course then calls foo() again...until you stackoverflow.

    In case 2 you're effectively passing a "reference" to foo, saying "run this in 2s". Not actually calling foo().

    So use parens when you actually want to invoke it. Not when you want to refer to it.

提交回复
热议问题