Internet Explorer's function scoping oddities

After spending hours tracking down why the "Math" object was completely gone in Internet Explorer in one of my projects, I found out that if 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.

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.

tldr

Never use reserved words to name your variables, no matter how nested they are.

Last updated: 2009-11-16