JavaScript 变量定义:逗号与分号 [英] JavaScript variable definition: Commas vs. Semicolons

查看:28
本文介绍了JavaScript 变量定义:逗号与分号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在声明一组变量而不是分号时使用逗号有什么区别和/或优点(如果有的话).

What are the differences and/or advantages, if any, of using commas when declaring a group of variables rather than semicolons.

例如:

var foo = 'bar', bar = 'foo';

对比

var foo = 'bar';
var bar = 'foo';

我知道,如果您在第一个示例中的第一个变量上指定 var 关键字,它会在所有变量中持续存在,因此它们在作用域方面都会产生相同的最终结果.这只是个人喜好,还是两种方式都可以提高性能?

I know that if you specify the var keyword on the first variable in the first example it persists across all of the variables, so they both produce the same end result regarding scope. Is it just personal preference, or is there a performance benefit to doing it either way?

推荐答案

没有性能优势,只是个人选择和风格的问题.

No performance benefit, just a matter of personal choice and style.

第一个版本更简洁.

更新:

就通过网络传输的数据量而言,当然越少越好,但是您需要删除大量的 var 声明才能看到真正的影响.

In terms of the amount of data going over the wire, of course less is better, however you would need a hell of a lot of removed var declarations in order to see a real impact.

Minification 已经被提到作为第一个例子将有助于更好的东西然而,正如 Daniel Vassallo 在评论中指出的那样,一个好的压缩器无论如何都会自动为你做这件事,所以在这方面没有任何影响.

Minification has been mentioned as something that the first example will help with for better minification, however, as Daniel Vassallo points out in the comments, a good minifier will automatically do that for you anyways, so in that respect no impact whatsoever.

这篇关于JavaScript 变量定义:逗号与分号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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