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

查看:196
本文介绍了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 在评论中指出,一个好的minifier会自动为你做这件事,所以在这方面没有任何影响。

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天全站免登陆