What is the plus sign before a function?

This question already has an answer here:

  • JavaScript plus sign in front of function name 3 answers

  • It is normally used with IIFE/SIFE. When you use + sign like that, it evaluates the expression following that, so when you put it in a function, it executes even an anonymous function, like this

    +function(){
        console.log("Welcome");
    }()
    

    Output

    Welcome
    

    It is another way to get the same behavior when the entire function is enclosed with parenthesis, like this

    (function(){
        console.log("Welcome");
    }());
    

    Note : Not only + , any unary arithmetic operator will give the same result.


    It's called unary plus operator :

    The unary plus operator precedes its operand and evaluates to its operand but attempts to converts it into a number, if it isn't already. For example, y = +x takes the value of x and assigns that to y; that is, if x were 3, y would get the value 3 and x would retain the value 3; but if x were the string "3", y would also get the value 3

    链接地址: http://www.djcxy.com/p/94926.html

    上一篇: 引导函数声明

    下一篇: 函数之前的加号是什么?