这三种模块模式有什么区别? [英] What are the differences between these three types of module pattern?

查看:80
本文介绍了这三种模块模式有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1)function () { 
    // code here...

}();

2)(function () { 
    // code here...

})();



3)(function () { 
    // code here...

}());

有什么区别(特别是第三种变体)?它们都是一样的吗?

What are the differences (especially third variant)? Are they all the same?

推荐答案

第一个给出语法错误。第二个和第三个版本定义匿名函数并立即执行它。第二个和第三个版本也称为立即调用函数表达式。

First one gives a syntax error. Second and third versions define a anonymous function and immediately execute it. Second and third versions are also called Immediately Invoked Function Expressions.

您可能还会遇到另一个看起来像这样的版本。这在功能上与第二版和第三版相同,但它只是否定了返回值。

You might also encounter another version which looks like this. This is equal in functionality to 2nd and 3rd version but it just negates the return value.

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

这篇关于这三种模块模式有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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