洗牌算法:
class SuperArray extends Array{
shuffle(){
console.log(this);// SuperArray(5) [ 1, 2, 3, 4, 5 ]
// 洗牌算法
for (let i = this.length - 1; i>0; i--){
const j = Math.floor(Math.random()*(i+1));
[this[i],this[j]] = [this[j],this[i]];
}
}
}
let a = new SuperArray(1,2,3,4,5);
a.shuffle();
console.log(a);// SuperArray(5) [ 2, 4, 3, 5, 1 ]