藍森林首頁 | 返回主頁 | 本站地圖 | 站內搜索 | 聯繫信箱 |
 您目前的位置:首頁 > 自由軟件 > 技術交流 > 應用編程


    

藍森林 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方法,
把原數組隨機打亂順序.




Copyright © 1999-2000 LSLNET.COM. All rights reserved. 藍森林網站 版權所有。 E-mail : webmaster@lslnet.com