What's the meaning of “()” in a function call?

前端 未结 4 2021
清酒与你
清酒与你 2020-12-29 05:53

Now, I usually call a function (that requires no arguments) with () like this:

myFunction(); //there\'s empty parens

Except in

4条回答
  •  [愿得一人]
    2020-12-29 06:34

    First of all, "()" is not part of the function name. It is syntax used to make function calls.

    First, you bind a function to an identifier name by either using a function declaration:

    function x() {
        return "blah";
    }
    

    ... or by using a function expression:

    var x = function() {
        return "blah";
    };
    

    Now, whenever you want to run this function, you use the parens:

    x();
    

    The setTimeout function accepts both and identifier to a function, or a string as the first argument...

    setTimeout(x, 1000);
    setTimeout("x()", 1000);
    

    If you supply an identifier, then it will get called as a function. If you supply an string, than it will be evaluated (executed).

    The first method (supplying an identifier) is preferred ...

提交回复
热议问题