我来自 PHP,那里的惯例是使用 4 个空格制表符。我注意到在很多示例(和项目引导代码)中使用了 2 个空格。我无法找到提到这些项目间距的明确风格指南,所以我想知道这是否真的是惯例。
最佳答案
首先让我说 - 根据语言(Javascript)的要求,这两个空格不需要(事实上,大多数时候你可以在末尾不使用;
)。 但是如果你的意思是问人们是否使用它?那么答案如下:
对于 Node - 它从 here 开始所以是 Node 的创建者(也是维护者和 BDFL)!! (Ryan-Dahl又名“仁慈的独裁者”)坚持使用2个空格。当然,他是创造者,所以每个人都有追随的倾向。
Tabs vs Spaces
Let's start with the religious problems first. Our benevolent dictator has chosen 2 space indention for the node core, so you would do well to follow his choice.
然后是TJ Holowaychuk Express、Jade、Stylus 和 Mocha 的作者引用自 here
has an extremely consistent style. TJ uses two spaces for indentation, semicolons to end lines, and formats commas at the start of a new line:
然后是Isaac Z. Schlueter ,npm 的作者!!
即使在Jade reference page上...所有代码或多或少都有 2 个空格。
所以采用这种做法是更好的事情,并且几乎在 Node/Express/Jade 的所有主要项目中。
但是恕我直言,只要您确信您的代码是正确的,谁会在乎呢。也许有一天你会写出更好、优雅的代码,并成为“仁慈的独裁者”;-)。
希望能回答这个问题:)
关于node.js - Node/Express/Jade 风格指南?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18169065/