JavaScript 中列出变量的差异 [英] Difference in listing variables in JavaScript

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

问题描述

<块引用>

可能的重复:
在 JavaScript 中声明多个变量

我想知道在 JavaScript(和 JS 库)中列出变量是否有区别.

示例:

var $this = $(this),$body = $("body"),$main-wrap = $("body > section.wrapper");

var $this = $(this);var $body = $("body");var $main-wrap = $("body > section.wrapper");

我一直认为区别只在于符号.第一个示例较短且执行起来可能更快.最近我做了一些测试,现在我不确定了.任何人都可以解决这个问题吗?

执行速度、结果、方法有区别吗?

解决方案

当我开始使用 jsLint.它总是抓住那个并说用逗号分隔更快.

仔细研究一下,似乎有很多消息来源同意这一点.

来自 jsLint #scope

<块引用>

建议每个函数使用一个 var 语句.

来自A List Apart:强>

<块引用>

var 语句定义了一个或多个变量.有时您会看到如下所示的代码:

var image = document.getElementById("myImage");var div = document.getElementById("myDiv");

<块引用>

这段代码定义了两个变量,一个紧接着另一个.我经常看到这种模式 10 或更多连续变量.这样做人为地夸大了你的代码,因为可以使用一个将多个 var 语句组合成一个逗号运算符:

var image = document.getElementById("myImage"),div = document.getElementById("myDiv");

<块引用>

这段代码还定义了两个变量并提供相同的初始化.但是,您已经保存了三个另一个 var 将花费的字节数.三个字节可能看起来不就像一件大事,但如果你能找到几十个地方目前有一个额外的 var 语句,节省真的可以增加

但是,

使用 中的多个 var 语句有两个相当令人信服的理由SO海报.这里

Possible Duplicate:
Declaring Multiple Variables in JavaScript

I was wondering whether there is a difference in listing variables in JavaScript (and JS libraries).

Example:

var $this = $(this),
    $body = $("body"),
    $main-wrap = $("body > section.wrapper");

OR

var $this = $(this);
var $body = $("body");
var $main-wrap = $("body > section.wrapper");

I always thought that the difference only lies in notation. The first example is shorter and possibly faster to execute. Recently I did some testing and now I am not sure anymore. Can anyone clear this up?

Is there a difference in execution speed, result, method?

解决方案

I broke the habit of using multiple var statements when I started using jsLint. It always caught that and said that it was faster to separate by a comma.

Looking into this more, there seem to be many sources that agree with this.

From jsLint #scope

It is recommended that a single var statement be used per function.

From A List Apart:

The var statement defines one or more variables. Sometimes you’ll see code that looks like this:

var image = document.getElementById("myImage");
var div = document.getElementById("myDiv");

This code defines two variables, one right after the other. I often see this pattern for 10 or more variables in a row. Doing so artificially inflates the size of your code because multiple var statements can be combined into one using a comma operator:

var image = document.getElementById("myImage"),
div = document.getElementById("myDiv"); 

This code also defines two variables and provides the same initialization. However, you’ve saved the three bytes that another var would have cost. Three bytes might not seem like a big deal, but if you’re able to find dozens of places where there’s currently an extra var statement, the savings can really add up.

However,

here are two fairly compelling reasons to use multiple var statements from SO posters. and here

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

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