For one of them the "()
" is inside, for the other one it is outside. Here they are:
其中一个的"()"在里面,另一个在外面。在这里,他们是:
var a = (function() {
return {
bla: function() {
console.log('a');
}
};
} () );
var b = (function() {
return {
bla: function() {
console.log('b');
}
};
}) ();
a.bla();
b.bla();
5 个解决方案
#1
9
There is no difference.
没有区别。
The [unnecessary] parenthesis are just in different places. The function declaration is already an expression due to where it is located. The parenthesis would make a difference, while still resulting in equivalent code, if the declaration was in a statement context (and ironically they would turn it back into an expression context), which it is not.
括号只是在不同的地方。函数声明已经是它所在位置的表达式了。插入语会产生不同的效果,但仍然会产生等价的代码,如果声明是在一个语句上下文中(具有讽刺意味的是,它们会把它转换回一个表达式上下文),而它不是。
The common use of parenthesis in a similar scenario is for self-invoking functions. The parenthesis are required in this case because
类似场景中括号的常见用法是用于自调用函数。在这种情况下括号是必需的,因为
function x () { alert("hi!") } ()
is parsed as
解析为
function x () { alert("hi!") }; ()
when it appears as a statement -- or "top level element of a block" -- where it is parsed as a "FunctionDeclaration". Thus the following form is often used:
当它作为语句(或“块的顶层元素”)出现时,它被解析为“FunctionDeclaration”。因此,经常使用以下形式:
(function () { alert("hi!") })()
This works because function ...
is no longer a statement, as above, but an expression (parsed as a "FunctionExpression"), and the expression can continue so the Automatic Semicolon Insertion does not occur as in the previous case. Also note that the function name can be omitted.
这是因为函数。不再是语句(如上所示),而是表达式(解析为“FunctionExpression”),表达式可以继续,这样就不会像以前那样自动插入分号。还要注意,可以省略函数名。
However, because in the post the function ...
appears in an on the right of an =
(in an "AssignmentExpression") it is therefore already in an expression context (is parsed as a "FunctionExpression") and thus no additional parenthesis are needed.
但是,因为在post函数中…出现在=的右边(在“AssignmentExpression”中),因此它已经在表达式上下文中(被解析为“FunctionExpression”),因此不需要额外的括号。
All of these are identical, but I prefer the 2nd form (for consistency within my code):
所有这些都是一样的,但是我更喜欢第二种形式(为了代码的一致性):
a = function () {} ()
b = (function () {}) ()
c = (function () {} ())
Happy coding.
快乐的编码。
#2
2
There is no real difference. Both work in the same way. If you want to pass JSLint, you will need to use the first pattern, where the invocation parentheses are inside the other set of parentheses. If you don't, you will get the following error:
没有什么区别。这两种方法都是一样的。如果要传递JSLint,则需要使用第一个模式,其中调用括号在另一组括号内。如果您不这样做,您将会得到以下错误:
Move the invocation into the parens that contain the function.
将调用移动到包含函数的语法中。
Also note that the first set of parentheses are not required. It will work without them, but again will fail JSLint:
还要注意,不需要第一组括号。如果没有它们,它将会工作,但JSLint会再次失败:
Wrap an immediate function invocation in parentheses to assist the reader in understanding that the expression is the result of a function, and not the function itself.
在括号中封装一个即时函数调用,以帮助读者理解表达式是函数的结果,而不是函数本身。
A couple of related questions:
几个相关的问题:
JSLint error: "Move the invocation into the parens that contain the function"
JSLint错误:“将调用移动到包含函数的语法中”
JSLint错误解决方案
#3
1
There is no practical difference, it's just a subtle difference in how you make the Javascript engine think of the function as a value.
没有什么实际的区别,只是让Javascript引擎将函数视为值的细微差别。
Using ( function(){} () );
you are causing the function to be a value because a statement can't start with a parenthesis. Using ( function(){} ) ();
you are using the parentheses to first evaluate the function as a value, then call it.
使用(函数){}()因为语句不能以括号开头,所以使函数成为一个值。使用(函数){}();使用圆括号首先计算函数的值,然后调用它。
#4
0
I think is the same difference of this:
我认为这是相同的区别:
var myObj = {bla:'blabla'};
var a = (myObj);
var b = myObj;
...no difference :)
…没有区别:)
#5
0
the ending brackets mean that : When a function is to be invoked immediately, the entire invocation expression should be wrapped in parens so that it is clear that the value being produced is the result of the function and not the function itself. (taken from here)
结尾括号的意思是:当要立即调用一个函数时,整个调用表达式都应该用括号括起来,这样就可以清楚地看到所生成的值是函数的结果,而不是函数本身。(从这里)
So, if you use {}(), the function is immediately executed and the variable is assigned with the function result.
因此,如果使用{}(),函数将立即执行,变量将与函数结果一起分配。
Then if you use ({})(), if there's a function between (), it is executed and the value is assigned to the variable.
然后,如果您使用({})(),如果()之间有一个函数,则执行它,并将值分配给变量。
They are the same thing if they contain the same functions in my opinion.
在我看来,如果它们包含相同的功能,它们是一样的。
#1
9
There is no difference.
没有区别。
The [unnecessary] parenthesis are just in different places. The function declaration is already an expression due to where it is located. The parenthesis would make a difference, while still resulting in equivalent code, if the declaration was in a statement context (and ironically they would turn it back into an expression context), which it is not.
括号只是在不同的地方。函数声明已经是它所在位置的表达式了。插入语会产生不同的效果,但仍然会产生等价的代码,如果声明是在一个语句上下文中(具有讽刺意味的是,它们会把它转换回一个表达式上下文),而它不是。
The common use of parenthesis in a similar scenario is for self-invoking functions. The parenthesis are required in this case because
类似场景中括号的常见用法是用于自调用函数。在这种情况下括号是必需的,因为
function x () { alert("hi!") } ()
is parsed as
解析为
function x () { alert("hi!") }; ()
when it appears as a statement -- or "top level element of a block" -- where it is parsed as a "FunctionDeclaration". Thus the following form is often used:
当它作为语句(或“块的顶层元素”)出现时,它被解析为“FunctionDeclaration”。因此,经常使用以下形式:
(function () { alert("hi!") })()
This works because function ...
is no longer a statement, as above, but an expression (parsed as a "FunctionExpression"), and the expression can continue so the Automatic Semicolon Insertion does not occur as in the previous case. Also note that the function name can be omitted.
这是因为函数。不再是语句(如上所示),而是表达式(解析为“FunctionExpression”),表达式可以继续,这样就不会像以前那样自动插入分号。还要注意,可以省略函数名。
However, because in the post the function ...
appears in an on the right of an =
(in an "AssignmentExpression") it is therefore already in an expression context (is parsed as a "FunctionExpression") and thus no additional parenthesis are needed.
但是,因为在post函数中…出现在=的右边(在“AssignmentExpression”中),因此它已经在表达式上下文中(被解析为“FunctionExpression”),因此不需要额外的括号。
All of these are identical, but I prefer the 2nd form (for consistency within my code):
所有这些都是一样的,但是我更喜欢第二种形式(为了代码的一致性):
a = function () {} ()
b = (function () {}) ()
c = (function () {} ())
Happy coding.
快乐的编码。
#2
2
There is no real difference. Both work in the same way. If you want to pass JSLint, you will need to use the first pattern, where the invocation parentheses are inside the other set of parentheses. If you don't, you will get the following error:
没有什么区别。这两种方法都是一样的。如果要传递JSLint,则需要使用第一个模式,其中调用括号在另一组括号内。如果您不这样做,您将会得到以下错误:
Move the invocation into the parens that contain the function.
将调用移动到包含函数的语法中。
Also note that the first set of parentheses are not required. It will work without them, but again will fail JSLint:
还要注意,不需要第一组括号。如果没有它们,它将会工作,但JSLint会再次失败:
Wrap an immediate function invocation in parentheses to assist the reader in understanding that the expression is the result of a function, and not the function itself.
在括号中封装一个即时函数调用,以帮助读者理解表达式是函数的结果,而不是函数本身。
A couple of related questions:
几个相关的问题:
JSLint error: "Move the invocation into the parens that contain the function"
JSLint错误:“将调用移动到包含函数的语法中”
JSLint错误解决方案
#3
1
There is no practical difference, it's just a subtle difference in how you make the Javascript engine think of the function as a value.
没有什么实际的区别,只是让Javascript引擎将函数视为值的细微差别。
Using ( function(){} () );
you are causing the function to be a value because a statement can't start with a parenthesis. Using ( function(){} ) ();
you are using the parentheses to first evaluate the function as a value, then call it.
使用(函数){}()因为语句不能以括号开头,所以使函数成为一个值。使用(函数){}();使用圆括号首先计算函数的值,然后调用它。
#4
0
I think is the same difference of this:
我认为这是相同的区别:
var myObj = {bla:'blabla'};
var a = (myObj);
var b = myObj;
...no difference :)
…没有区别:)
#5
0
the ending brackets mean that : When a function is to be invoked immediately, the entire invocation expression should be wrapped in parens so that it is clear that the value being produced is the result of the function and not the function itself. (taken from here)
结尾括号的意思是:当要立即调用一个函数时,整个调用表达式都应该用括号括起来,这样就可以清楚地看到所生成的值是函数的结果,而不是函数本身。(从这里)
So, if you use {}(), the function is immediately executed and the variable is assigned with the function result.
因此,如果使用{}(),函数将立即执行,变量将与函数结果一起分配。
Then if you use ({})(), if there's a function between (), it is executed and the value is assigned to the variable.
然后,如果您使用({})(),如果()之间有一个函数,则执行它,并将值分配给变量。
They are the same thing if they contain the same functions in my opinion.
在我看来,如果它们包含相同的功能,它们是一样的。