Gre*_*gir 1 javascript variables
我是jQuery以外的JavaScript新手,我正在阅读JavaScript数组中的随机化以及使用随机数的Array.sort方法的缺点.我看到建议改为使用Fisher-Yates shuffle.在查看此方法的JavaScript代码时:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
我对这条线感到震惊:
var i = this.length, j, temp;
Run Code Online (Sandbox Code Playgroud)
这里发生了什么?变量是赋予多个值,还是这个简写?
变量永远不能同时具有多个值.
你提供的代码是简写
var i = this.length;
var j;
var temp;
Run Code Online (Sandbox Code Playgroud)
像上面这样的语法在大多数编程语言中都是合法的.