在JavaScript中声明多个变量在JavaScript中,可以声明如下所示的多个变量:var variable1 = "Hello World!";var variable2 = "Testing...";var variable3 = 42;.或者像这样var variable1 = "Hello World!",
variable2 = "Testing...",
variable3 = 42;一种方法比另一种更好/更快吗?
3 回答
潇湘沐
TA贡献1816条经验 获得超6个赞
除了可维护性外,第一种方法消除了意外事故全局变量创建的可能性:
(function () {
var variable1 = "Hello World!" // semicolon is missed out accidently
var variable2 = "Testing..."; // still a local variable
var variable3 = 42;
}());
而第二种方式则不那么宽容:
(function () {
var variable1 = "Hello World!" // comma is missed out accidently
variable2 = "Testing...", // becomes a global variable
variable3 = 42; // a global variable as well
}());
达令说
TA贡献1821条经验 获得超6个赞
var
添加回答
举报
0/150
提交
取消