views:

103

answers:

2

I think I've found a bug in IE's (IE8) handling for the for-in javascript statement. After several hours of boiling this down to a small example, it looks like IE specifically skips any property called "toString" in a for-in loop - regardless of whether it is in a prototype or is an "Own Property" of the object.

I've placed my test code here:

function countProps(obj) {
    var c = 0;
    for (var prop in obj) {
        c++;
    }
    return c;
}

var obj = {
    toString: function() {
        return "hello";
    }
};

function test() {
    var o = "";
    var d = document.getElementById('output');

    o += "<br/>obj.hasOwnProperty('toString') == " + obj.hasOwnProperty('toString');
    o += "<br/>countProps(obj) = " + countProps(obj);
    o += "<br/>obj.toString() = " + obj.toString();

    d.innerHTML = o;
}

This should produce:

obj.hasOwnProperty('toString') == true
countProps(obj) = 1
obj.toString() = hello

but in IE, I'm getting:

obj.hasOwnProperty('toString') == true
countProps(obj) = 0
obj.toString() = hello

This special casing of any property called 'toString' is wrecking havoc with my code that tries to copy methods into a Function.prototype - my custom toString function is always skipped.

Does anyone know a work-around? Is this some sort of quirks-mode only behavior - or just a BUG?

+4  A: 

Yes, it is a bug. See this answer.

Quoting CMS:

Another well known JScript bug is the "DontEnum Bug", if an object in its scope chain contains a property that is not enumerable (has the { DontEnum } attribute), if the property is shadowed on other object, it will stay as non-enumerable, for example:

var dontEnumBug = {toString:'foo'}.propertyIsEnumerable('toString');

It will evaluate to false on IE, this causes problems when using the for-in statement, because the properties will not be visited.

CD Sanchez
@Daniel, Thanks for quoting my answer, I will post some workaround to the problem in the other thread -and maybe more implementation bugs- ;)
CMS
Thanks. So, contrary to the JavaScript spec, this is a case where a user defined property is not enumerable. Is there a list of all such properties so I can add a special test to my code when dontEnumBug is true?
mckoss
I updated my test code:http://startpad.googlecode.com/hg/labs/js/misc/tostring.htmlA work around should specifically test for the presence of properties named: toString, toLocaleString, valueOf, constructor, and isPrototypeOf, from my tests of IE8.
mckoss
+1  A: 

This is a bug in IE, and also applies to properties named valueOf.

You can work around it like this:

if(obj.toString !== Object.prototype.toString || obj.hasOwnProperty("toString"))
    //Handle it
if(obj.valueOf !== Object.prototype.valueOf || obj.hasOwnProperty("valueOf"))
    //Handle it
SLaks
It applies to others too. Here's one list: https://developer.mozilla.org/en/ECMAScript_DontEnum_attribute#JScript_DontEnum_Bug
CD Sanchez