【问题标题】:When do I use parentheses and when do I not?When do I use parentheses and when do I not?
【发布时间】:2022-12-27 12:07:44
【问题描述】:

How come I can say:

var myFunction = function() {
   setTimeout(myFunction, 1000);
}
myFunction();

Why does the function call in the setTimeout not require parentheses, but the last line does?

【问题讨论】:

    标签: javascript


    【解决方案1】:

    Nutshell

    • myFunction references the function
    • myFunction() calls the function

    More Words

    setTimeout expects a function reference*as an argument.

    There are circumstances where setTimeout(myFunction(), 1000) might make sense, like if myFunction() returns a function, e.g.

    function myFunction() {
        return function() {
            alert("ohai")
        }
    }
    
    // Or
    
    const myFunction = () => () => alert("ohai")
    

    So:

    setTimeout(myFunction(), 1000);
    
    • setTimeout gets the return value of myFunction
    • myFunction returns a function (that calls alert)

    meaning there will be an alert every second.

    See also Why function statement requires a name?

    *Or a string to be evaluated, but a reference is preferred.

    【讨论】:

    • Weird. JavaScript is a quirky language.
    • @cf_PhillipSenn: It is basically the same in Python or C (function pointers). Only providing the name of the function references it. Adding parenthesis behind it calls it. Of course this does not exist in languages where functions are not first class objects and therefore cannot be referenced directly (such as Java).
    • It's not that weird :) you can do that sort of thing in C#, VB.NET too. It's a very useful feature.
    【解决方案2】:

    myFunction is a function

    myFunction() calls the function and yields whatever value the function returns.

    The purpose of setTimeout is running code after some time elapses. You need to pass just the function to it (so setTimeout can itself call the function when appropriate) because if you called the function (with the parenthesis) before passing it to setTimeout it would executenowinstead of after 1 second.

    【讨论】:

      【解决方案3】:

      When you use the parenthesis, it's saying 'call this function now'. So if you say setTimeout(myFunction(),1000);, it will use thereturn valueof the function as the callback for the timeout. If the return value for the function is not itself a function, you'll get an error because it will try to execute something that isn't executable after the timeout (a string, a number, undefined, etc).

      【讨论】:

      • Although, worth noting: If the string that was returned was something that was executable, like say alert("Hello world!"); it would work in this context because setTimeout can accept strings as js code literals.
      【解决方案4】:

      In line 2, the function myFunction is not called, but passed as an argument to the setTimeout function, whereas in line 4 myFunction is called; to call a function, youalwayshave to use parentheses, even if there are no arguments.

      【讨论】:

      • I think a 25 watt light bulb just went on.
      【解决方案5】:

      I think this example would make it clearer if i may,

      function callback() {
        console.log('this function runs on page loads.');
      }
      
      setTimeout(callback(), 2000); 
      

      Here callback() function will run immediately after page loads and won't wait 2 seconds.

      function callback() {
        console.log('this function runs after page loads.');
      }
      
      setTimeout(callback, 2000);
      

      Here callback() function will run after 2 seconds.

      【讨论】:

      • Yes, I agree. The first example is wrong and should never be used.
      猜你喜欢
      • 2022-12-02
      • 2022-11-20
      • 2019-07-14
      • 2022-12-28
      • 2022-12-02
      • 2013-07-10
      • 2022-12-04
      • 2022-12-27
      • 2022-12-27
      相关资源
      最近更新 更多