JavaScript !function(){}
This question already has an answer here:
!function(a){/* ... */}();
Using an unary operator to invoke an IIFE is common practice. That's a common shorthand for:
(function(a){/* ... */}());
or:
(function(a){/* ... */})();
You can also substitute the not unary operator with any other unary operator:
-function(a){ /* ... */ }();
+function(a){ /* ... */ }();
/* ... etc. */
gives a good explaination for function invocation https://github.com/airbnb/javascript/issues/44#issuecomment-13063933
!function () {}();
is equivalent to
(function(){})();
except the author is saving 1 byte of code.
In many cases, it's about saving bytes.
!function aaa(){}()
!function bbb(){}();
is the same as this:
!function aaa(){}()
;(function bbb(){})();
notice the ";" in that last bit. That is defensive, as it protects your code a bit from runaway js that might preceed it.
funny, I asked this same question some time ago:
Came across a convention I've never seen. What does it do? !function
great reference on it: What does the exclamation mark do before the function?
链接地址: http://www.djcxy.com/p/17424.html上一篇: 奇怪的匿名javascript函数调用