I have a problem with Javascript Literal functions.
for (curitem in array)
{
var tl = new GTileLayer(copyrightCollection, 0, 21 );
tl.getTileUrl = function(a, b) {
return MyFunction(a, b, curitem);
};
...
secondArray.push(tl);
}
The problem is: When “MyFunction” is called, the third parameter has always the last value used in function creation (values are like 0,1,2,3,4,6 and I always receive 6).
How can I solve this?
Thank you
Classic problem with loops and closures. You can easily solve it by creating a new variable every loop, e.g. by using an anonymous function and passing the value as a parameter: