What does the exclamation mark do before the funct

2018-12-30 23:54发布

!function () {}();

10条回答
看风景的人
2楼-- · 2018-12-31 00:50

! is a logical NOT operator, it's a boolean operator that will invert something to its opposite.

Although you can bypass the parentheses of the invoked function by using the BANG (!) before the function, it will still invert the return, which might not be what you wanted. As in the case of an IEFE, it would return undefined, which when inverted becomes the boolean true.

Instead, use the closing parenthesis and the BANG (!) if needed.

// I'm going to leave the closing () in all examples as invoking the function with just ! and () takes away from what's happening.

(function(){ return false; }());
=> false

!(function(){ return false; }());
=> true

!!(function(){ return false; }());
=> false

!!!(function(){ return false; }());
=> true

Other Operators that work...

+(function(){ return false; }());
=> 0

-(function(){ return false; }());
=> -0

~(function(){ return false; }());
=> -1

Combined Operators...

+!(function(){ return false; }());
=> 1

-!(function(){ return false; }());
=> -1

!+(function(){ return false; }());
=> true

!-(function(){ return false; }());
=> true

~!(function(){ return false; }());
=> -2

~!!(function(){ return false; }());
=> -1

+~(function(){ return false; }());
+> -1
查看更多
还给你的自由
3楼-- · 2018-12-31 00:52

! will negate (opposite) whatever you're expecting as a result, i.e if you have

var boy = true;
undefined
boy
true
!boy
false

when you call boy, your result will be true, but the moment you add the ! when calling boy, i.e !boy, your result will be false. Which in other words you mean NotBoy, but this time it's basically a boolean result, either true or false.

That's the same thing that happens to the !function () {}(); expression, running only function () {}(); will flag you an error, but add ! right in front of your function () {}(); expression, makes it the opposite of the function () {}(); which should return you true. Example can be seen below:

function () {}();
SyntaxError: function statement requires a name
!function () {}();
true
查看更多
低头抚发
4楼-- · 2018-12-31 00:53

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 !

查看更多
梦醉为红颜
5楼-- · 2018-12-31 00:55

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 () {})()
查看更多
登录 后发表回答