I then waste half an hour trying to remember how to get around them, struggling to squint at the various StackOverflow answers to see how they relate to my own loop. So, here is a step-by-step example, that hopefully will make sense to me next time I hit this.
I'm using CasperJS here, but that is not too important. Just consider those lines as "something that gets executed later but is using local variables".
Here is the before:
var url="http://example.com/";And here is the intermediate stage:
var A=['a','b','c'];
var B=['','99'];
for(var ix1 = 0;ix1 < A.length;++ix1){
for(var ix2 = 0;ix2 < B.length;++ix2){
var label = A[ix1] + "-" + B[ix2];
casper.test.begin(label, {
test:function(test){ runTheTests(test,url,B[ix2],A[ix1]); }
});
}
}
var url="http://example.com/";Then you need to pass into that new function anything outside it that is changing on each pass of the loop. I.e. anything involving ix1 or ix2. It ends up looking like this:
var A=['a','b','c'];
var B=['','99'];
for(var ix1 = 0;ix1 < A.length;++ix1){
for(var ix2 = 0;ix2 < B.length;++ix2)(function f(){
var label = A[ix1] + "-" + B[ix2];
casper.test.begin(label, {
test:function(test){ runTheTests(test,url,B[ix2],A[ix1); }
});
})();
}
var url="http://example.com/";
var A=['a','b','c'];
var B=['','99'];
for(var ix1 = 0;ix1 < A.length;++ix1){
for(var ix2 = 0;ix2 < B.length;++ix2)(function f(a,b){
var label = a + "-" + b;
casper.test.begin(label, {
test:function(test){ runTheTests(test,url,b,a); }
});
})(A[ix1],B[ix2]);
}
No comments:
Post a Comment