最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavaScript: can a variable have multiple values? - Stack Overflow

programmeradmin0浏览0评论

I'm fairly new to JavaScript beyond jQuery, and I was reading up on randomization in a JavaScript array & the shortings of using the Array.sort method with a random number. I see the remendation instead is to use the Fisher-Yates shuffle. In looking at the JavaScript code for this method:

Array.prototype.randomize = function()
{
    var i = this.length, j, temp;
    while ( --i )
    {
        j = Math.floor( Math.random() * (i - 1) );
        temp = this[i];
        this[i] = this[j];
        this[j] = temp;
    }
}

I'm struck by this line:

var i = this.length, j, temp;

What's going on here? Is a variable being given multiple values, or is this shorthand for something?

I'm fairly new to JavaScript beyond jQuery, and I was reading up on randomization in a JavaScript array & the shortings of using the Array.sort method with a random number. I see the remendation instead is to use the Fisher-Yates shuffle. In looking at the JavaScript code for this method:

Array.prototype.randomize = function()
{
    var i = this.length, j, temp;
    while ( --i )
    {
        j = Math.floor( Math.random() * (i - 1) );
        temp = this[i];
        this[i] = this[j];
        this[j] = temp;
    }
}

I'm struck by this line:

var i = this.length, j, temp;

What's going on here? Is a variable being given multiple values, or is this shorthand for something?

Share Improve this question edited Aug 12, 2013 at 18:53 Felix Kling 817k181 gold badges1.1k silver badges1.2k bronze badges asked Aug 12, 2013 at 18:42 GregirGregir 1,5745 gold badges21 silver badges43 bronze badges 1
  • This is going on: es5.github.io/#x12.2. – Felix Kling Commented Aug 12, 2013 at 18:49
Add a ment  | 

6 Answers 6

Reset to default 5

A variable can never have multiple values at the same time.

The code you give is shorthand for

var i = this.length;
var j;
var temp;

Syntax like that above is legal in most programming languages.

var i = this.length, j, temp;

is the same as :

var i = this.length;
var j; // here the value is undefined
var temp; // same, temp has a an undefined value

No, it's the shorthand for: var i = this.length; var j; var temp;

You are creating three variables, and only the leftmost will be born with a value - in this case, whatever the value this.length is.

As pointed out by everyone else respondig to you question, it's the same as:

var i = this.length;
var j, temp;

Other languages like Java, C# and Visual Basic allow you to create variables with a similar syntax. I.E.:

I.e.:

// C# and Java
int i = this.length, j, temp;

// which is the same as:
int i = this.length;
int j, temp;

 

' Visual Basic
Dim i = this.length as Integer, j as Integer, temp as Integer

' Which is the same as:
Dim i = this.length as Integer
Dim j as Integer, temp as Integer

It's just multiple declaration of variables in a single line. It's equivalent to this:

var i, j, temp;
i = this.length;

Which is equivalent to this:

var i;
var j;
var temp;
i = this.length;

The specification defines the variables statement to be the var keyword, followed by a list of variables declarations, separated by a ma:

VariableStatement :
    var VariableDeclarationList ;

VariableDeclarationList :
    VariableDeclaration
    VariableDeclarationList , VariableDeclaration

Note the recursive definition of VariableDeclarationList. It means that an unlimited number of variables declarations can follow a var keyword.

Hence

var foo, bar;

is the same as

var foo;
var bar;

Related question: What is the advantage of initializing multiple javascript variables with the same var keyword?

发布评论

评论列表(0)

  1. 暂无评论