2

I've run into a very odd issue with IE8's JS engine (possibly previous versions as well, but NOT IE9 in IE8 mode since the JS engine doesn't fallback). Simplified example:

var foo = { toString : 42, x : 22 };
for(var n in foo){ 
    console.log(n)
}

// result: "x"

In other words, the toString property never gets enumerated. Nor would valueOf, hasOwnProperty, etc... or var x = 5; x.toFixed = 42;

So any property that natively exists can not be enumerated as far as I can tell, even after you replace it...

My question -- Does anyone know of any way to actually access these?!? I need to because I'm walking the prototype of an object and the toString function isn't getting picked up.

  • Just a little correction: you are not replacing those inherited properties, but just *shadowing* them. I had to ask [this question](http://stackoverflow.com/questions/5373278/variable-shadowing-in-javascript) to get this term right. – Šime Vidas Sep 09 '11 at 21:25
  • Unfortunately I don't have IE8 anymore (at all), I would love to be able to test this for myself. What does `foo.toString` return? What does `foo.hasOwnProperty( 'toString' )` return? – Šime Vidas Sep 09 '11 at 21:28
  • @Šime Vidas - Okay, shadowing :) `foo.toString === 42`, `foo.hasOwnProperty( 'toString' ) === true` –  Sep 09 '11 at 22:21
  • Found it! It's called the [JScript DontEnum Bug](https://developer.mozilla.org/en/ECMAScript_DontEnum_attribute#JScript_DontEnum_Bug). – Šime Vidas Sep 09 '11 at 23:05
  • @ŠimeVidas -- can you post an answer? :) –  Dec 15 '11 at 23:15

1 Answers1

2

So, the behavior you're experiencing in IE is the so-called "JScript DontEnum Bug" which exists in IE8 and below.

In IE < 9, JScript will skip over any property in any object where there is a same-named property in the object's prototype chain that has the DontEnum attribute.

Source: https://developer.mozilla.org/en/ECMAScript_DontEnum_attribute#JScript_DontEnum_Bug

Šime Vidas
  • 182,163
  • 62
  • 281
  • 385