How to call a self executing function in JavaScript?

后端 未结 4 1275
天涯浪人
天涯浪人 2020-12-14 10:14

When I have some code I need to execute more than once I wrap it up in a function so I don\'t have to repeat myself. Sometimes in addition there\'s a need to execute this co

4条回答
  •  醉梦人生
    2020-12-14 10:51

    The second function foo is a function definition expression.

    In a function definition expression, you can only call the function with the name within the function itself according to "Javascript the definitive guide":

    For function definition expressions, the name is optional: if present, the name refers to the function object only within the body of the function itself.

    It can be used in a recursion function definition expression.

    For example:

    var f = function factorial(n) {
        if (n == 0 || n == 1)
            return 1;
        else
            return n * factorial(n-1);
    }
    

提交回复
热议问题