究竟如何!function(){}()有效吗? [英] How exactly does !function(){}() work?

查看:274
本文介绍了究竟如何!function(){}()有效吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过:

!function(){ //code }();

在多个地方用于立即执行匿名功能。通常情况下,它代替:

Used in several places to immediately execute an anonymous function. Normally, it's used in lieu of:

(function(){ //code }())

任何人都知道实际上如何使函数执行?

Anyone know how the ! actually makes the function execute?

推荐答案

什么!

What the ! does

当您使用时,该函数成为该函数的单个操作数一元(逻辑)NOT 运算符。

When you use !, the function becomes the single operand of the unary (logical) NOT operator.

这会强制将函数作为表达式进行求值,这样就可以立即内联调用它。

This forces the function to be evaluated as an expression, which allows it to be invoked immediately inline.

其他选择

Other alternatives

您几乎可以使用任何其他选项运营商。以下是一些示例...

You can do this with just about any operator. Here are some examples...

'invoke',function(){ /*code*/ }();
1+function(){ /*code*/ }();
void function(){ /*code*/ }();
~function(){ /*code*/ }();
+function(){ /*code*/ }();

其中一些好处是操作符的含义不会过载。

Nice thing about some of these is that the meaning of the operator is not overloaded.

() 的问题

当你在函数周围使用()时,你可以遇到一些会出现的错误如果你连续多个没有用分号分隔它们。

When you use () around the function, you can hit some bugs that will crop up if you have more than one in a row without a semicolon separating them.

(function() {
    alert('first');
}())


(function() {
    alert('second');
}())

// TypeError: undefined is not a function

这将导致 TypeError ,因为第二个函数周围的外部()对将被解释为打算调用一个功能。第一个当然没有返回函数,所以你试图调用 undefined

This will result in a TypeError, because the pair of outer () around the second function will be interpreted as intending to call a function. The first one doesn't return a function of course, so you're trying to call on undefined.

如何使用其他运营商处理(或避免)问题

How using a different operator copes with (or avoids) the problem

即使是像 + 这样的运算符,在某种程度上会重载也不会导致错误。

Even an operator like +, which is overloaded to some degree won't cause an error.

如果你这样做......

If you do this...

+function() {
    alert('first');
}()

+function() {
    alert('second');
}()

第一个 + 被解释为一元+运算符,并且它转换从第一个函数返回的结果,在这种情况下是 undefined 所以它得到转换为 NaN

The first + is interpreted as a unary + operator, and it converts the result returned from the first function, which in this case is undefined so it gets converted to NaN.

第二个 + 将是解释为加法运算符,因此会尝试将 NaN 添加到第二个函数的返回结果中,这又是 undefined

The second + will be interpreted as the addition operator, and so will try to add NaN to the return result of the second function, which again here is undefined.

结果当然是 NaN ,但它是无害的。抛出错误没有非法代码。

The result of course is NaN, but it is harmless. There's no illegal code to throw an error.

演示操作符如何与函数交互

Demonstration of how the operators interact with the functions

为了证明这一点,只需给每个函数一个返回值,然后将其粘贴到控制台中......

To prove this, just give each function a return value, then paste it into the console...

+function() {
    alert('first');
    return "10";
}()

+function() {
    alert('second');
    return 20;
}()

// 30

你会得到两个警告,然后控制台将显示 30 ,因为第一个 + 运算符将String 10转换为Number 10 ,第二个 + 将两个结果加在一起。

You'll get the two alerts, and then the console will show 30 because the first + operator converts the String "10" to the Number 10, and the second + added the two results together.

这篇关于究竟如何!function(){}()有效吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆