分号在自我调用功能之前? [英] Semicolon before self-invoking function?

查看:138
本文介绍了分号在自我调用功能之前?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JavaScript中使用自调用函数之前使用分号有什么好处。我在几个流行的jQuery插件中看到了这种方法,我很想知道这是否是我不知道的JavaScript中的下一个很棒的东西。提前谢谢,伙计们!

What is the benefit of using semicolon before a self-invoking function in JavaScript. I saw this approach in few popular jQuery plugins and I'm curious to find if this is the next awesome thing in JavaScript that I dont know. Thanks in advance, guys!

推荐答案

如果将两个文件连接在一起,自动调用函数如下所示:

If you concatenate two files with self-invoking functions together that look like this:

文件A:

(function(){...A...})()

文件B:

(function(){...B...})()

文件A + B:

(function(){...A...})()(function(){...B...})()

你有两个陈述没有分隔符。当您将文件合并在一起然后缩小它们时会发生这种情况。

You have two statements without separator. This happens when you cat files together and then minify them.

现在文件B的作者在前面放了一个分号:

Now the author of file B puts a semicolon in front:

文件B2:

;(function(){...B2...})()

你会得到一个有效的脚本:

And you'll get a working script:

(function(){...A...})();(function(){...B2...})()

这篇关于分号在自我调用功能之前?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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