我是从 PHP 过来的,约定是使用 4 个空格制表符。我注意到在很多示例(和项目引导代码)中使用了 2 个空格。我还没有找到明确的风格指南,提到这些项目的间距,所以我想知道这是否真的是惯例。
2 回答
Let me start by saying - according to the requirements of the language (Javascript) the 2 spaces are NOT REQUIRED (infact most of the times you can get away without using a ;
too at the end). However if you mean to ask whether people use it ? Then here is the answer :
For Node - it starts from here so the creator of Node (and also maintainer and BDFL) !! (Ryan-Dahl aka the "benevolent dictator") insisted on using the 2 spaces . Ofcourse he is the creator so everybody has a tendency to follow.
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.
Then comes TJ Holowaychuk author of Express, Jade, Stylus, and Mocha quoted from 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:
Then there is Isaac Z. Schlueter,author of npm !!
Even on the Jade reference page ... all the code more or less has 2 spaces.
So it is a better thing to adopt the practice and it is almost in all major projects of Node/Express/Jade.
But IMHO who cares as long as you are confident that your code is correct. Maybe one day you will write better and elegant code and become the "benevolent dictator" ;-).
Hope that answers the question :)
Find the below urls this can be helpful if you are new to these: http://jade-lang.com/api/