I'm looking at Douglas Crockford's Code Conventions for JavaScript document, and he's saying that var
s should be alphabetical, and one per line.
我查看了Douglas Crockford的JavaScript文档代码约定,他说vars应该按字母顺序排列,每行一个。
var a; // array of class names
var c = node.className; // the node's classname
var i; // loop counter
However, the jsLint (and jsHint) standard is to declare them on a single line, and it throws this error if done Crockford's way
然而,jsLint(和jsHint)标准是在一行中声明它们,如果按照Crockford的方式执行,它会抛出这个错误
too many var statements
太多的var语句
Therefore, jsLint wants it done like this.
因此,jsLint希望这样做。
var a, c = node.className, i;
This seems quite contradictory to me, and though probably quite minute in the overall scope of programming, I'm hoping to get this right before I get it wrong.
这对我来说似乎很矛盾,虽然在编程的总体范围中可能只有很短的一段时间,但我希望在出错之前能把它做对。
What is the generally accepted practice when declaring JavaScript vars?
在声明JavaScript vars时,普遍接受的实践是什么?
6 个解决方案
#1
13
LINT wants a single var
declaration statement, but it can be spread over multiple lines.
LINT需要一个单独的var声明语句,但是它可以分布在多个行上。
var a, b, c;
or
或
var a,
b,
c;
The reason it wants a single statement is to avoid any confusion about which variables belong to the local scope. With a single var statement, all locally scoped variables are contained to a single location within the scope and anyone can read the code quickly to see what they are.
它需要单个语句的原因是为了避免对哪些变量属于本地范围的混淆。使用一个var语句,所有局部作用域内的变量都包含在作用域内的一个位置上,任何人都可以快速地阅读代码以查看它们是什么。
It is further recommended that this declaration statement be at the top of the scope, since the JavaScript hoisting mechanism moves them there before execution anyway. By writing your code to expect that statement at the top of the scope, the hoisting mechanism can't cause any unexpected behavior.
还建议将这个声明语句放在范围的顶部,因为JavaScript提升机制在执行之前将它们移动到那里。通过编写代码以期望在范围的顶部使用该语句,提升机制不会导致任何意外行为。
#2
3
Neither way is wrong, but I generally prefer to use single line style declarations at the beginning of a code block. The most important thing is to keep it consistent. Example:
这两种方法都不对,但我通常更喜欢在代码块的开头使用单行样式声明。最重要的是保持一致。例子:
function() {
var foo = "bar",
number = 123,
etc = "...";
// stuff..
}
#3
3
Even a combination is fine. This is all about preference. If you are working with a team, then ask about the preference and keep it consistent.
即使是组合也可以。这都是关于偏好。如果你和一个团队一起工作,那么询问他们的偏好并保持一致。
var a,
c = node.className,
i;
#4
2
The argument is mostly a style discussion, however, it is far more important to agree on a standard and have your team use it consistently.
争论主要是一种风格的讨论,然而,更重要的是对一个标准达成一致,并让您的团队始终如一地使用它。
#5
2
Another option (it's the one I use) is:
另一个选项(我用的就是这个)是:
var a = ...,
b = ...,
c, d;
This makes it clear how many variables I'm declaring and that they're all related in some way (since I'm declaring them in the same space, they basically always are). It also helps differentiate between variables that already have values and ones that don't. That said, I only declare variables without assigning very rarely.
这就清楚地表明了我声明了多少变量,它们都以某种方式相关(因为我在同一个空间中声明它们,它们基本上都是)。它还有助于区分已经有值的变量和没有值的变量。也就是说,我只声明变量而很少赋值。
Unless I'm working with code that follows a different standard, this is what I do. And that's the most important thing: follow the style of your project. Otherwise, go by your preference. If you really don't care, use my style ;)
除非我使用的代码遵循不同的标准,否则这就是我所做的。这是最重要的一点:遵循项目的风格。否则,按你的喜好去做。如果你真的不在乎,那就用我的风格;
#6
0
You can declare multiple variables and assign them in the same line. It is a matter of personal preference.
您可以声明多个变量并在同一行中分配它们。这是个人喜好的问题。
#1
13
LINT wants a single var
declaration statement, but it can be spread over multiple lines.
LINT需要一个单独的var声明语句,但是它可以分布在多个行上。
var a, b, c;
or
或
var a,
b,
c;
The reason it wants a single statement is to avoid any confusion about which variables belong to the local scope. With a single var statement, all locally scoped variables are contained to a single location within the scope and anyone can read the code quickly to see what they are.
它需要单个语句的原因是为了避免对哪些变量属于本地范围的混淆。使用一个var语句,所有局部作用域内的变量都包含在作用域内的一个位置上,任何人都可以快速地阅读代码以查看它们是什么。
It is further recommended that this declaration statement be at the top of the scope, since the JavaScript hoisting mechanism moves them there before execution anyway. By writing your code to expect that statement at the top of the scope, the hoisting mechanism can't cause any unexpected behavior.
还建议将这个声明语句放在范围的顶部,因为JavaScript提升机制在执行之前将它们移动到那里。通过编写代码以期望在范围的顶部使用该语句,提升机制不会导致任何意外行为。
#2
3
Neither way is wrong, but I generally prefer to use single line style declarations at the beginning of a code block. The most important thing is to keep it consistent. Example:
这两种方法都不对,但我通常更喜欢在代码块的开头使用单行样式声明。最重要的是保持一致。例子:
function() {
var foo = "bar",
number = 123,
etc = "...";
// stuff..
}
#3
3
Even a combination is fine. This is all about preference. If you are working with a team, then ask about the preference and keep it consistent.
即使是组合也可以。这都是关于偏好。如果你和一个团队一起工作,那么询问他们的偏好并保持一致。
var a,
c = node.className,
i;
#4
2
The argument is mostly a style discussion, however, it is far more important to agree on a standard and have your team use it consistently.
争论主要是一种风格的讨论,然而,更重要的是对一个标准达成一致,并让您的团队始终如一地使用它。
#5
2
Another option (it's the one I use) is:
另一个选项(我用的就是这个)是:
var a = ...,
b = ...,
c, d;
This makes it clear how many variables I'm declaring and that they're all related in some way (since I'm declaring them in the same space, they basically always are). It also helps differentiate between variables that already have values and ones that don't. That said, I only declare variables without assigning very rarely.
这就清楚地表明了我声明了多少变量,它们都以某种方式相关(因为我在同一个空间中声明它们,它们基本上都是)。它还有助于区分已经有值的变量和没有值的变量。也就是说,我只声明变量而很少赋值。
Unless I'm working with code that follows a different standard, this is what I do. And that's the most important thing: follow the style of your project. Otherwise, go by your preference. If you really don't care, use my style ;)
除非我使用的代码遵循不同的标准,否则这就是我所做的。这是最重要的一点:遵循项目的风格。否则,按你的喜好去做。如果你真的不在乎,那就用我的风格;
#6
0
You can declare multiple variables and assign them in the same line. It is a matter of personal preference.
您可以声明多个变量并在同一行中分配它们。这是个人喜好的问题。