Go back to main site

Internet Explorer's function scoping oddities

introduction

I spent a couple hours the other day at work tracking why the "Math" object was completely gone in Internet Explorer. The reason for that is that someone defined a function as object property and labeled it "Math". Even though "Math" wasn't assigned, IE considered it global and wiped out the built-in "Math" object.

sample code

var myObject = {
  myFunc: function Math() {
    var a = 1;
  }
}
alert(Math.round(1.2));

Internet Explorer here will manage to lose reference to the Math object. You can find a list of words to avoid at this URL: http://www.javascripter.net/faq/reserved.htm. I'd be curious to see, according to ECMA standards who is right, IE or the half-dozen other browsers out there. Anyways... if you lose a built-in class one day, don't just hunt at the top level, it could be a sneaky function declared but unassigned.

comments

No comment yet...

Add a comment








Last Update: 2009-11-16 · Bertrand Janin <tamentis@neopulsar.org> · Powered by Python & Debian GNU/Linux