当我开始使用jsLint时,我打破了使用多个var
语句的习惯。它总是抓住这一点,并说用逗号分隔会更快。
对此进行更多研究,似乎有很多消息来源同意这一点。
来自jsLint #scope
建议每个函数使用单个 var 语句。
从列表分开:
var 语句定义一个或多个变量。有时你会看到如下代码:
var image = document.getElementById("myImage");
var div = document.getElementById("myDiv");
This code defines two variables, one
right after the other. I often see this pattern for 10 or more
variables in a row. Doing so artificially inflates the size of your
code because multiple var statements can be combined into one using a
comma operator:
var image = document.getElementById("myImage"),
div = document.getElementById("myDiv");
This code also defines two variables
and provides the same initialization. However, you’ve saved the three
bytes that another var would have cost. Three bytes might not seem
like a big deal, but if you’re able to find dozens of places where
there’s currently an extra var statement, the savings can really add
up.
However,
here are two fairly compelling reasons to use multiple var
statements from SO posters. and here