Wednesday, September 22, 2010

Javascript closure compiler error.

Google closure compiler converts

var jsonvar;
eval('jsonvar = '+msg.d+');
for(var i = 0;i<jsonvar.length;i++)
{
dochicken(jsonvar[i].Chicken);
}

as

for(d = 0;d < (void 0).length;d++) {
dochicken((void 0)[d].Chicken);
}


So don't declare your json ajax receptor before usage.

Revised according to comment:
Closure can start to do weird things if you include variable names in eval text.

This is a better solution from John:


var jsonvar = eval("(" + msg.d+ ")");
for(var i = 0;i{
dochicken(jsonvar[i].Chicken);
}
CHIKEN();CHIKEN();

2 comments:

  1. This is a better solution:
    var jsonvar = eval("(" + msg.d+ ")");
    for(var i = 0;i<jsonvar.length;i++)
    {
    dochicken(jsonvar[i].Chicken);
    }

    ReplyDelete