|
藍森林 http://www.lslnet.com 2006年8月25日 8:28
請教下面代碼的含義
[code]
Array.prototype.random=function() {
var a=this;
var l=a.length;
for(var i=0;i<l;i++) {
var r=Math.floor(Math.random()*(l-i));
a=a.slice(0,r).concat(a.slice(r+1)).concat(a[r]);
}
return a;
}
[/code] |
幫你頂一下 看看其他高手 的 意思 |
設置基數,得到隨機數 |
[html]
<script>
Array.prototype.random=function() {
var a=this;
var l=a.length;
for(var i=0;i<l;i++) {
var r=Math.floor(Math.random()*(l-i));
a=a.slice(0,r).concat(a.slice(r+1)).concat(a[r]);
}
return a;
}
var arr=new Array(1,2,3,4,5,6,7,8);
arr=arr.random();
alert(arr);
</script>
[/html]
代碼的意思是:
在基對像Array下定義random方法,
把原數組隨機打亂順序. |
|