Javascript 自调用函数之前的分号?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7365172/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 01:58:44  来源:igfitidea点击:

Semicolon before self-invoking function?

javascriptjquery

提问by 7elephant

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!

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

回答by amoebe

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

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

File A:

文件A:

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

File B:

文件乙:

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

File A+B:

文件 A+B:

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

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

你有两个没有分隔符的语句。当您将文件放在一起然后缩小它们时会发生这种情况。

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

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

File B2:

文件 B2:

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

And you'll get a working script:

你会得到一个工作脚本:

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

回答by Mike Samuel

Self-invoking functions are surrounded by parentheses, and in JavaScript parentheses are overloaded to mean

自调用函数被括号包围,在 JavaScript 中括号被重载的意思是

  1. Grouping expressions to override precedence: (x + y) * z
  2. Function application : f()
  1. 分组表达式以覆盖优先级: (x + y) * z
  2. 功能应用: f()

Putting a semicolon before the function prevents the function from becoming an argument to whatever precedes it when the parentheses become confused with function application.

当括号与函数应用程序混淆时,在函数前放置分号可防止函数成为它前面任何内容的参数。

Consider

考虑

var x = 42

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

is the same as

是相同的

var x = 42(function () { ... })()

but

var x = 42

;

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

is the same as

是相同的

var x = 42;

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

回答by Mike Samuel

Iwrite allJavaScript in a semicolon-free style. When writing without semicolons at the end of every line, due to Automatic Semicolon Insertion (ASI), there are a few special cases that can "be confusing" at first:

以无分号的风格编写所有JavaScript。当每行末尾没有分号时,由于自动分号插入 (ASI),有一些特殊情况最初可能会“令人困惑”:

  1. Starting a top-level expression with an operator, a ((open parenthesis) in this case, which like most other operators, can continuethe previous expression and thus suppresses the "automatic insertion of a semicolon". (This generally only occurswhen using a self invoking function.)

  2. Just kidding about #2: there isn't one! (Learn only one rule and you too can enjoy life without extra semicolons ;-)

  1. 使用 operator 开始顶级表达式,(在这种情况下是一个(左括号),与大多数其他 operator 一样,可以继续前一个表达式,从而抑制“自动插入分号”。(这通常在使用自调用函数时发生。)

  2. 开个玩笑#2:没有!(只学习一个规则,你也可以享受没有额外分号的生活;-)

Since I write in a semicolon-free style I thus alwayswrite it as (where the function-expression can naturally span multiple lines):

由于我以无分号的风格编写,因此我总是将其写为(函数表达式可以自然地跨越多行):

;(FunctionExpression)()

In mycase it isn't about "safety" or trying to "catch an error" (honestly, if your style is to use semi-colons and you forget a semi-colon, then you've already created the error elsewhereand writing a ;at the start for "safety" is hogwash). No; in my case it is done for consistencywith knowledgeof my chosen style and "knowing" that starting a line with an operatorcan continue an expression from a previous line.

我的情况下,这与“安全”或试图“捕捉错误”无关(老实说,如果您的风格是使用分号而您忘记了分号,那么您已经在其他地方创建了错误并写作;“安全”开头的a是胡说八道)。不; 在我的情况下,这样做是为了与我选择的样式的知识保持一致,并且“知道”使用运算符开始一行可以从前一行继续表达式。

See JavaScript: Semicolon Insertion (Everything You Need To Know)for the details (it is by far the best article I have seen on the subject).

有关详细信息,请参阅JavaScript: Semicolon Insertion (Everything You Need To Know)(这是迄今为止我在该主题上看到的最好的文章)。

Happy coding.

快乐编码。

回答by Francis

For me, the semi colon was triggering an error in Internet Explorer 8 (or at least that's what IETester said), and preventing the ui tabs from working properly.

对我来说,分号在 Internet Explorer 8 中触发了错误(或者至少 IETester 是这么说的),并阻止了 ui 选项卡正常工作。

The error message was Invalid character in jquery.all.ui.js Line: 1. Char: 1.

错误消息是Invalid character in jquery.all.ui.js Line: 1. Char: 1

I stumbled on the semi-colon completely by chance. When I removed the ;from the ;(function($)it worked, seemingly without side-effects or loss of functionality. I am using Drupal, don't know whether this has anything to do with the matter.

我完全偶然发现了分号。当我;;(function($)它的工作中删除它时,似乎没有副作用或功能损失。我正在使用Drupal,不知道这是否与此事有关。