Wow this is some odd looking code.
in javascript don't define vars in loops, it makes the code harder to read. Declare all the variables that will be used in the funcion at the beginning of the function, as variables in javascript are function-wide, not closure-wide.
function ParseData(data) {
actors = eval(data);
for (i = 0; i < actors.length; i++) {
CF.widget("LBL_DESC_" + (i+1)).label = actors[i].description;
var color;
actors[i].state > 0 ? color = 0x00ffff : color = 0xffffff;
CF.widget("LBL_ICON_" + (i+1)).color = color;
SetLineVisibility((i+1), true);
var pon = function(id) {
return function() {
System.print("pon = " + id);
}
} (actors[i].id);
CF.widget("BTN_ON_" + (i+1)).onPress = pon;
}
}
I rearranged it a bit so i't easyer to read. Are you sure this is what you wanted to write?
- actors is an implied global variable.
- i is an implied global variable
- color and pon should be defined at the beginning od ParseData
- the closure pon looks strange to me. In javascript closures are usually written (function () {})(); the extra braces are for some obscure reason I can't really remember now.
- you assign the "closure" to an action of a button press.
- the return statement looks odd, should be just return {....}
This is either some high level coding and I'm making an ass of myself for replying to this or something that is not written as should have been.
I find it strange that you provide the id as an argument to the pon function but not to the return function. Is id in pon even accessible from the return function? The message from the simulatroe would imply that no, it's not accessible.