(function(){})() 之间的区别;和函数(){}(); [英] Difference between (function(){})(); and function(){}();

查看:27
本文介绍了(function(){})() 之间的区别;和函数(){}();的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
是“(function ( ) { } ) ( )”和 “(function ( ) { } ( ) )”在 JavaScript 中功能相同?

这是我还没有完全弄清楚的事情,但我一直在使用 function(){}() 只是因为如果我添加括号,我的 VIM 语法高亮就会搞砸,尽管我已经看到 (function(){})() 出现了很多次,也许是 IE 的问题?

This is something I haven't quite figured out yet, but I have been using function(){}() just because my VIM syntax highlight screws up if I add the parenthesis, although I've seen (function(){})() around many times, maybe its an IE thing?

var singleton = function() {
    // code
}();

var singleton = (function() {
    // code
})();

推荐答案

Peter Michaux 讨论了 一对重要的父母.

Peter Michaux discusses the difference in An Important Pair of Parens.

基本上括号是一种约定,表示紧随其后的是立即调用的函数表达式,而不是普通函数.特别是如果函数体很长,这会减少惊喜,

Basically the parentheses are a convention to denote that an immediately invoked function expression is following, not a plain function. Especially if the function body is lengthy, this reduces surprises,

这篇关于(function(){})() 之间的区别;和函数(){}();的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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