js将数组元素随机排序的方法

在群里看见的一个面试题,试了一下,还是可以做出来的,但是需要查资料,主要是岁一些方法了解的不清楚,可能这个跟我平时不太注重基础理论有关系,像什么构造函数啊,我根本就不关心什么叫构造函数,我一直都以为我只要会用就可以了,看来面试题还是不能只会用,还要会基础知识的,抱着犀牛书看去了,不说了,我反正不是一个面试高手

<script type="text/javascript">
Array.prototype.shuffle=function(){
    _this=this;
    this.re=[];
    this.t=this.length;
    for(var i=0;i<this.t;i++){
        (function(i){
            var temp=_this;
            var m=Math.floor(Math.random()*temp.length);
            _this.re[i]=temp[m];
            _this.splice(m,1);
        })(i)    
    }
    return this.re
}


var arr=[0,1,2,3,4,5,6,7];
var arrs=["00","11","22","33","44","55","66","77"];
alert(arrs.shuffle());
alert(arr.shuffle());
</script>
原文地址:https://www.cnblogs.com/busicu/p/4018975.html