Categories
main

Backward compatibility dillema

Open your favorite browser. Type

javascript:for(var i in null) {}

and check if this will throw an error.
In fact, it should, but I tested Konqueror, Opera, Firefox, Shimono tested IE6 and Shaver tested Safari – all of them passed this.

To prove that this behavior is not proper, head to ECMA standard 262, and take a look at:
Point 12.6.3, step 3:

12.6.4 The for-in Statement
The production IterationStatement : for ( LeftHandSideExpression in Expression ) Statement is
evaluated as follows:
1. Evaluate the Expression.
2. Call GetValue(Result(1)).
3. Call ToObject(Result(2)).
4. Let V = empty.

Point 9.9:

9.9 ToObject

The operator ToObject converts its argument to a value of type Object according to the following table:
Input Type Result
Undefined Throw a TypeError exception.
Null Throw a TypeError exception.
Boolean Create a new Boolean object whose [[value]] property is set to the value of the boolean. See 15.6 for a description of Boolean objects.
Number Create a new Number object whose [[value]] property is set to the value of the number. See 15.7 for a description of Number objects.
String Create a new String object whose [[value]] property is set to the value of the string. See 15.5 for a description of String objects.
Object The result is the input argument (no conversion).

So we should throw an error. The question is. What sites will we break? Shaver asked me to find some sites that will be broken, but it’s not that easy. So if you know about any site that might do for in loop on undefined element, please, let me know.