如何将匿名函数的值返回到数组中。我已经尝试过这段代码,但它不起作用。哪里是我错了添加匿名函数返回到aray push
for (var i = 0; i < num; i++) {
//get ramdom color
// arr.push(randomColor());
arr.push(function() {
//pick a "red" from 0 - 255
var r = Math.floor(Math.random() * 256);
//green 0 - 255
var g = Math.floor(Math.random() * 256);
//blue 0 -255
var b = Math.floor(Math.random() * 256);
return "rgb" + "(" + r + ", " + g + ", " + b + ")";
});
}
为什么不'arr.push(randomColor());'? – Andreas
我已经写了randomcolor(),但我只想尝试匿名函数。谢谢。 –