Now I hate Javascript too!

This is some code I wrote to evaluate a form with structured data in it on the client side, attempting to build the same object heirarchy Ruby on Rails would build once the form is submitted.

Unfortunately I got stuck for ages getting this right thanks to 2 very very stupid Javascript quirks.

  1. at line 10 I was previously using for (var n in names), and this actually caused the code in line 21 to fail unexpectedly with the array lookup always returning null. The fact using the for…in construct on an array does not give you the value but does not give you an incrementable index either was something I had no reason to anticipate.
  2. A well known type checking error I had forgotten about since last time it bit me: typeof Array returns ‘object’ and not ‘array’. Unbelievably dumb. Hence my use of the helper function is_array in line 14.

That both these problems were occurring at the same time made it very very difficult for me to see why the code was failing, until I had added debug trace messages to practically every line to try to work out what was going wrong.

It doesn’t help that I am also quite sleepy and haven’t washed in a few days since my hot water system failed on the weekend– It’s now fixed and I am about to enjoy a good long shower.

One Response:

  1. anthony says:

    I remember using the for(in) method, and ending up iterating through the list of members in the prototype. Can’t remember what I was doing, but very easy to run off the tracks in javascript.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>