简体   繁体   English

Node.js中是否需要逗号?

[英]Are commas necessary in Node.js?

Are there risks incurred when omitting commas in variable declarations for node.js? 在node.js的变量声明中省略逗号是否会产生风险? For example, declaring some global variables like the following works just fine: 例如,声明一些像下面这样的全局变量就可以了:

express = require('express')
jade = require('jade')

And I don't want to write commas if it's safe not to write them (I don't care about "beauty/clarity of code" arguments). 如果不写它们是安全的,我不想写逗号(我不关心“代码的美/清晰度”)。

Important : I mean commas, not semicolons (got 3 answers about semicolons). 重要 :我的意思是逗号,而不是分号(有关分号的3个答案)。 It's perfectly OK and even recommended to remove semicolons from node.js. 它完全没问题,甚至建议从node.js中删除分号。 The creator of npm also does it: http://blog.izs.me/post/3393190720/how-this-works npm的创建者也是这样做的: http//blog.izs.me/post/3393190720/how-this-works

If in doubt, check the latest javascript specs: http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf 如有疑问,请查看最新的javascript规范: http//www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf

Note that you also don't need to write 请注意,您也不需要写

var

for global variables. 对于全局变量。

But this question is about "commas" so please don't replace commas by semicolons by mistake when editing my question (done before). 但是这个问题是关于“逗号”的,所以请不要在编辑我的问题(之前完成)时用分号替换逗号。

In JavaScript, if you don't write semicolons ; 在JavaScript中,如果你不写分号 ; they will be inserted for you, invisibly. 它们将被无形插入。 And you may not always like where they go. 你可能并不总是喜欢他们去的地方。

You are not technically required to end every statement with a semicolon. 从技术上讲,您不需要以分号结束每个语句。 However, most consider it a good idea. 但是,大多数人认为这是一个好主意。

For more info, peruse through the results of this google search . 有关详细信息,请仔细阅读此Google搜索的结果。 We've been arguing about this topic for a long time. 我们长期以来一直在争论这个话题。


Here's an example of why this is more complex than it appears at first glance. 这是一个为什么这比第一眼看上去更复杂的例子。 While you are not technically required to end every statement with a semicolon, there are a few cases where you MUST, or things break. 虽然技术上并不要求用分号结束每个语句,但在某些情况下你必须或事情中断。 You cannot completely omit them in most codebases. 在大多数代码库中,您无法完全省略它们。

foo.def = bar
(function() {
  // some self executing closure
})()

Looks simple enough right? 看起来很简单吧? Well the interpreter looks at that and does this: 那么翻译就会看到这个并做到这一点:

foo.def = bar(function() {
  // some self executing closure
})()

Which is probably not what you were expecting. 这可能不是你所期待的。 The way to fix it, is with a semicolon. 修复它的方法是使用分号。

foo.def = bar;
(function() {
  // some self executing closure
})()

There a lot of cases like this. 有很多这样的案例。 You can either learn them all, and only use them in those cases, and when you inevitably forget you try to debug your code that's doing something so strange and bizarre that you tear your hair out hours... "what do you mean wtfvar is not a function?!? It's not supposed to be a function!" 你可以全部学习它们,也只能在这些情况下使用它们,当你不可避免地忘记你试图调试你的代码时,你正在做一些如此奇怪和奇怪的事情,你会把你的头发拉出来...... “你是什么意思wtfvar是不是一个功能?!?它不应该是一个功能!“

Or you can just use semicolons with consistency. 或者你可以使用具有一致性的分号。

In short, no. 简而言之,没有。 The problems you are likely to run into will arise when you go to do things like code minifying and the compiler thinks that your two statements are one. 当你去做代码缩小和编译器认为你的两个陈述是一个时,你可能遇到的问题就会出现。 Anyhow, if you choose not to use commas/semicolons, which is absolutely not recommended, you should be fine. 无论如何,如果你选择不使用逗号/分号,绝对不推荐,你应该没问题。

Node.js uses the V8 engine to read your code so it will pretty much behave like in Google Chrome. Node.js使用V8引擎来读取您的代码,因此它几乎与Google Chrome中的行为相似。 That said, not using semicolons is generaly a bad practice. 也就是说,不使用分号通常是一种不好的做法。 The interpreter will try to understand your code and may be sometime mistaken (by your fault). 解释器将尝试理解您的代码,并且可能有时会弄错(由您的错误)。

Check this out for a full explanation: Do you recommend using semicolons after every statement in JavaScript? 请查看此内容以获得完整说明: 您是否建议在JavaScript中的每个语句后使用分号?

This very late answer just goes to clear confustion for others that might read this. 这个非常晚的答案只是为那些可能读到这一点的其他人提供了明确的混淆。

Since you're actually talking about commas , not semi-colons, I can only assume that you have a misunderstanding of what is implicitly being added by the engine. 既然你实际上是在谈论逗号而不是分号,我只能假设你误解了引擎隐含的内容。

Commas are not optional. 逗号不是可选的。 And this code: 这段代码:

express = require('express')
jade = require('jade')

is being implicitly converted into this: 正在隐式转换为:

var express = require('express');
var jade = require('jade');

not this, which you might be expecting: 不是这个,你可能会期待:

var express = require('express'),
    jade = require('jade');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM