IE8 bug in for-in JavaScript statement?
Asked Answered
N

2

9

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?

Nygaard answered 14/9, 2010 at 0:35 Comment(0)
U
10

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.

Upgrade answered 14/9, 2010 at 0:42 Comment(3)
Thanks for quoting my answer, I will post some workaround to the problem in the other thread -and maybe more implementation bugs- ;)Yangyangtze
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?Nygaard
I updated my test code: startpad.googlecode.com/hg/labs/js/misc/tostring.html A work around should specifically test for the presence of properties named: toString, toLocaleString, valueOf, constructor, and isPrototypeOf, from my tests of IE8.Nygaard
B
2

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
Busk answered 14/9, 2010 at 0:57 Comment(2)
It applies to others too. Here's one list: developer.mozilla.org/en/…Upgrade
had the same problem with includes. if(obj.includes !== Object.prototype.includes || obj.hasOwnProperty("includes")) { //handleit }Aerial

© 2022 - 2024 — McMap. All rights reserved.