我是Javascript菜鸟。同时读取模块模式。我注意到了这个匿名函数,它的函数作用域中有括号。我以前没有用过这个。我想更好地理解它。
// first example
(function(){
//this is IIFE I always use to avoid globle var. I think the simple form of this is F();
})();
// second example
(function () {
//However, what is this concept? what's the formal name of this function?
}());这两者之间的主要区别是什么?我是如何理解第二个例子的?
发布于 2015-01-31 05:30:31
通常你不需要换行括号,如果你去掉它们,你会看到它们是一样的:
function(){}()
function(){}()上面,那已经是生活了。
但是,如果该函数没有用作表达式,例如在赋值中,那么JavaScript会认为它是一个函数声明。要消除代码的歧义并强制执行表达式,您可以执行各种操作,如添加括号:
// Same thing
(function(){}())
(function(){})()或者使用一元运算符:
!function(){}()
+function(){}()
void function(){}()https://stackoverflow.com/questions/28244885
复制相似问题