JavaScript 表达式中的三个分号有什么作用?

发布于 2024-09-10 09:31:43 字数 138 浏览 6 评论 0原文

我在一些表达式中到处看到了三个分号。
它有任何逻辑效果吗?

我见过的最接近的解释是它告诉 Dean Edwards 压缩器忽略该行。

;;; var someVar = 'Rebel';

I've seen a triple semicolon in a few expressions here and there.
Does it have any logical effect?

The closest thing I've seen for an explanation is that it tells the Dean Edwards compressor to ignore that line.

;;; var someVar = 'Rebel';

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

吻安 2024-09-17 09:31:43

它让人们在 StackOverflow 上提问。

除此之外,它什么也不做。

It makes people ask questions on StackOverflow.

Other than that, it does nothing.

坏尐絯℡ 2024-09-17 09:31:43

没有什么。绝对没有。

三个分号、十个分号、一百个分号,它们都被解释为相同的结果:

Nothing. Absolutely nothing.

Three semicolons, ten semicolons, a hundred semicolons, they all get interpreted to the same result: nothing.

美男兮 2024-09-17 09:31:43

以三个分号开头的行用于调试代码:它表明这些行不应出现在生产环境中。 Javascript 通过压缩器或其他算法运行,在创建优化的 JS 文件时删除 ;;; 行。

;;; console.log("only run this line when debugging!");

如上所述,三个分号实际上在 Javascript 中没有任何作用:它只是结束三个连续的空语句。如果使用了实际的注释,

// console.log("only run this line when debugging!");

那么当您想进入调试模式时,您必须手动删除所有注释,然后在完成后将它们放回去。另一个解决方案是创建一个 DEBUG 变量并将所有调试行包装在一个条件中:

var DEBUG = true;
if(DEBUG){
     console.log("only run this line when debugging!");
}

但这有点麻烦,并且实际上将不需要的代码添加到您的 Javascript 文档中。当然,您可以通过压缩器运行 JS 来删除 DEBUG 条件,但此时您不妨只使用 ;;; 方法,这样更简单。

有关现实生活中的示例,请参阅此问题。顺便说一句,我认为语法来自 emacs

Lines starting with three semicolons are there for debug code: it indicates that those lines should not appear in the production environment. The Javascript is run through a compressor or some other algorithm that removes ;;; lines when creating the optimized JS file.

;;; console.log("only run this line when debugging!");

As indicated above, three semicolons actually does nothing in Javascript: it just ends three consecutive empty statements. If an actual comment was used

// console.log("only run this line when debugging!");

then you'd have to go in and manually remove all comments when you wanted to enter debug mode, and then go in and put them back when you were done. The other solution is to create a DEBUG variable and wrap all debug lines in a condition:

var DEBUG = true;
if(DEBUG){
     console.log("only run this line when debugging!");
}

but this is a little cumbersome and actually adds unneeded code to your Javascript document. Of course you could run the JS through a compressor to remove the DEBUG conditions, but at that point you might as well just use the ;;; method, which is simpler.

See this question for a real life example of this. BTW, I think the syntax comes from emacs.

疧_╮線 2024-09-17 09:31:43

结束空语句 3 次。

Ends an empty statement 3 times.

平定天下 2024-09-17 09:31:43

它们是空洞的陈述,没有任何作用。解释器或编译器可能会删除它们,除非语法需要语句。

They are empty statements and have no effect. It is possible that the interpreter or compiler will remove them unless a statement is required by the syntax.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文