问题描述
在 JavaScript 中,可以像这样声明多个变量:
In JavaScript, it is possible to declare multiple variables like this:
var variable1 = "Hello, World!";
var variable2 = "Testing...";
var variable3 = 42;
...或者像这样:
var variable1 = "Hello, World!",
variable2 = "Testing...",
variable3 = 42;
一种方法是否比另一种更好/更快?
Is one method better/faster than the other?
推荐答案
第一种方式更容易维护.每个声明都是一行上的单个语句,因此您可以轻松地添加、删除和重新排序声明.
The first way is easier to maintain. Each declaration is a single statement on a single line, so you can easily add, remove, and reorder the declarations.
使用第二种方式,删除第一个或最后一个声明很烦人,因为它们分别从 var
关键字开始并以分号结束.每次添加新声明时,都必须用逗号替换最后一行中的分号.
With the second way, it is annoying to remove the first or last declaration because they start from the var
keyword and finish with the semicolon respectively. Every time you add a new declaration, you have to replace the semicolon in the last old line with a comma.
这篇关于在 JavaScript 中声明多个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!