how does javascript !function() { } work

This question already has an answer here:

  • Why do you need to invoke an anonymous function on the same line? 19 answers
  • JavaScript plus sign in front of function name 3 answers
  • Javascript anonymous function call [duplicate] 4 answers

  • With a return value.. you negate that with !

    var x=!function(){return false}();
    console.log(x);
    // true
    

    double negation

    var pizza='pizza';
    var x=!!function(){return pizza}();
    console.log(x);
    // true
    
    // returns true if pizza is defined, not 'pizza'
    // returns false if pizza is ''.
    

    demo

    http://jsfiddle.net/9shzF/1/


    Pretty much as with any other thing. The anonymous function is autoexecuted, therefore returns a value, and the value is negated.

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

    上一篇: 在JavaScript中的匿名函数前徘徊〜

    下一篇: javascript!function(){}如何工作