ECMAScript

Inconsistent implementation of the for ... in loop.

I ran into a hard-to-find bug today. After a while I figured out that IE cleared one of my hashes, for seemingly no good reason. This was my code:

  1. for (var p in hash) {
  2. hash["x_" + p] = hash[p];
  3. delete hash[p];
  4. }

I actually didn’t manage to reproduce this behavior outside of my original script, instead IE would go into an infinite loop and rapidly claim all available memory when the code ran.

Unfortunate Syntax Rules

There are two things with the JavaScript syntax that really bothers me. What they have in common is that they force you to remember to add superfluous characters to your code. This makes it really easy to forget to add them, and this may cause syntax errors or bugs.

Sound familiar? Probably. First of all I’m thinking of the fact that array and object literal values need to be comma separated. It makes sense, [1, 2, 3] looks good when you write it on one line. But take a look at the multi-line version:

  1. var a = [
  2. 1,
  3. 2,
  4. 3
  5. ];

ECMAScript Array Instantiation

There are two ways of instantiating Arrays in EMCAScript (if you exclude every conceivable hack that you could pull out of your hat just to show me that there are other ways as well). One[^1] uses the regular object instantiation syntax, namely new Array and it’s also possible to omit the new in order to call Array as a function instead (the latter is simply an alias of the former as can be seen in the specification). The other way is leaner on the fingers and is written as [], called the literal notation[^2].

Syndicate content