0
我遇到了一些函数数组的问题。当我运行下面的代码时,它将该函数添加到数组中,但它不会更改函数内部的索引值。任何帮助将不胜感激。谢谢从for循环生成一个javascript函数数组
var dlinks = [];
function getLinks(index) {
return function() {
this.echo('report link September');
casper.start(reportList[index]);
casper.thenOpen(reportList[index], function() {
casper.capture('data1.png');
var dlUrl = reportList[index] + '&csv=true';
this.download(dlUrl , reportDates[index].substring(0,2) + myfile, "GET");
});
}
}
console.log(getLinks(1));
for (var i = 0; i < reportList.length; i ++) {
dlinks.push(getLinks(i));
}
使你这样想是什么行为?我看起来没问题。你打印了实际的索引吗? – DrC
是的console.log(getLinks(1)):打印该函数,但它的变量索引保持不变。它应该是1.原因是我没有实际调用函数,直到生成了dlinks数组之后。 – user3474649
但是当你稍后调用它们(通过dlinks)时,你是否已经在生成的函数中完成了console.log(“Index is”+ index)。 – DrC