Friday, March 10, 2006

Loose typing is a bitch

A few weeks ago I created a bug in my Javascript by assigning a variable something like this:

function foo(mynum) {
var x = num;
... proceeed to do use x as if it were a number...
}

But x was being interpreted as a string, which made the code fail. Eventually I figured out what was happening, and changed the code to this:

function foo(mynum) {
var x = num*1;
...
}

which fixed the problem. This made me recall why I hate loose typing.

Well, today I did it again! I was setting a Laszlo button's "enabled" attribute as follows:

...
btn.setAttribute("enabled", "false");
...

Mysteriously this was not working. I hunted everywhere for other places in the code where the attribute was being set differently! And eventually I found the problem - to correctly set a boolean you had better not put it in quotes. Like this:

...
btn.setAttribute("enabled", false);
...

Ouch!

0 Comments:

Post a Comment

<< Home