我有带有“ this”的代码,如果没有“ this”,如何编写相同的代码?
var memory_array = ["red", "red", "green", "green", "blue", "blue", "brown", "brown", "yellow", "yellow", "gray", "gray", "white", "white","black","black"];
var memory_values = [];
var memory_tile_ids = [];
var tiles_flipped = 0;
Array.prototype.memory_tile_shuffle = function(){
var i = this.length, j, temp;
while(--i > 0){
j = Math.floor(Math.random() * (i+1));
temp = this[j];
this[j] = this[i];
this[i] = temp;
}
}
答案 0 :(得分:0)
一种实现方法是将数组作为参数传递给函数,然后仅使用数组对其进行处理。
function memory_tile_shuffle(array){
// You can also use a third argument array in forEach if you prefer
array.forEach(function(item, i) {
var j = Math.floor(Math.random() * (i+1));
temp = array[j];
array[j] = array[i];
array[i] = temp;
});
}