What does the exclamation mark do before the function?

后端 未结 9 1892
误落风尘
误落风尘 2020-11-21 04:47
!function () {}();
相关标签:
9条回答
  • 2020-11-21 04:49

    It returns whether the statement can evaluate to false. eg:

    !false      // true
    !true       // false
    !isValid()  // is not valid
    

    You can use it twice to coerce a value to boolean:

    !!1    // true
    !!0    // false
    

    So, to more directly answer your question:

    var myVar = !function(){ return false; }();  // myVar contains true
    

    Edit: It has the side effect of changing the function declaration to a function expression. E.g. the following code is not valid because it is interpreted as a function declaration that is missing the required identifier (or function name):

    function () { return false; }();  // syntax error
    
    0 讨论(0)
  • 2020-11-21 04:49

    Its just to save a byte of data when we do javascript minification.

    consider the below anonymous function

    function (){}
    

    To make the above as self invoking function we will generally change the above code as

    (function (){}())
    

    Now we added two extra characters (,) apart from adding () at the end of the function which necessary to call the function. In the process of minification we generally focus to reduce the file size. So we can also write the above function as

    !function (){}()
    

    Still both are self invoking functions and we save a byte as well. Instead of 2 characters (,) we just used one character !

    0 讨论(0)
  • 2020-11-21 04:54

    Exclamation mark makes any function always return a boolean.
    The final value is the negation of the value returned by the function.

    !function bool() { return false; }() // true
    !function bool() { return true; }() // false
    

    Omitting ! in the above examples would be a SyntaxError.

    function bool() { return true; }() // SyntaxError
    

    However, a better way to achieve this would be:

    (function bool() { return true; })() // true
    
    0 讨论(0)
  • 2020-11-21 04:56

    Its another way of writing IIFE (immediately-invoked function expression).

    Its other way of writing -

    (function( args ) {})()
    

    same as

    !function ( args ) {}();
    
    0 讨论(0)
  • 2020-11-21 05:07

    JavaScript syntax 101. Here is a function declaration:

    function foo() {}
    

    Note that there's no semicolon: this is just a function declaration. You would need an invocation, foo(), to actually run the function.

    Now, when we add the seemingly innocuous exclamation mark: !function foo() {} it turns it into an expression. It is now a function expression.

    The ! alone doesn't invoke the function, of course, but we can now put () at the end: !function foo() {}() which has higher precedence than ! and instantly calls the function.

    So what the author is doing is saving a byte per function expression; a more readable way of writing it would be this:

    (function(){})();
    

    Lastly, ! makes the expression return true. This is because by default all IIFE return undefined, which leaves us with !undefined which is true. Not particularly useful.

    0 讨论(0)
  • 2020-11-21 05:07

    The function:

    function () {}
    

    returns nothing (or undefined).

    Sometimes we want to call a function right as we create it. You might be tempted to try this:

    function () {}()
    

    but it results in a SyntaxError.

    Using the ! operator before the function causes it to be treated as an expression, so we can call it:

    !function () {}()
    

    This will also return the boolean opposite of the return value of the function, in this case true, because !undefined is true. If you want the actual return value to be the result of the call, then try doing it this way:

    (function () {})()
    
    0 讨论(0)
提交回复
热议问题