JavaScript:一个变量可以有多个值吗? [英] JavaScript: can a variable have multiple values?
问题描述
我对 jQuery 之外的 JavaScript 还是很陌生,我正在阅读 JavaScript 数组中的随机化 &使用带有随机数的 Array.sort 方法的缺点.我看到的建议是使用 Fisher-Yates shuffle.查看此方法的 JavaScript 代码:
I'm fairly new to JavaScript beyond jQuery, and I was reading up on randomization in a JavaScript array & the shortcomings of using the Array.sort method with a random number. I see the recommendation 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;
}
}
我被这条线震惊了:
var i = this.length, j, temp;
这是怎么回事?一个变量是否被赋予了多个值,或者这是某种东西的简写?
What's going on here? Is a variable being given multiple values, or is this shorthand for something?
推荐答案
一个变量永远不能同时有多个值.
A variable can never have multiple values at the same time.
您提供的代码是简写
var i = this.length;
var j;
var temp;
上面的语法在大多数编程语言中都是合法的.
Syntax like that above is legal in most programming languages.
这篇关于JavaScript:一个变量可以有多个值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!