在JavaScript中声明多个变量 [英] Declaring Multiple Variables in JavaScript

查看:146
本文介绍了在JavaScript中声明多个变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JavaScript中,可以这样声明多个变量:

  var variable1 =Hello World! 
var variable2 =Testing ...;
var variable3 = 42;

...或类似:

  var variable1 =Hello World!,
variable2 =Testing ...,
variable3 = 42;一个方法比另一个方法更好/更快吗?


解决方案

第一种方式更容易维护。每个声明都是单个语句在一行,所以你可以很容易地添加,删除和重新排序声明。



第二种方式,它是恼人的删除第一个或最后一个声明,因为它们包含 var 关键字和分号。每次添加新声明时,您都必须将旧行中的分号更改为逗号。


In JavaScript, it is possible to declare multiple variables like this:

var variable1 = "Hello World!";
var variable2 = "Testing...";
var variable3 = 42;

...or like this:

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.

With the second way, it is annoying to remove the first or last declaration because they contain the var keyword and semicolon. And every time you add a new declaration, you have to change the semicolon in the old line to a comma.

这篇关于在JavaScript中声明多个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆