JavaScript function definition inside a with() block: Firefox is the oddball
February 14th, 2009I came across yet another browser discrepancy with a rather obscure scenario: A function declaration within a with block:
var obj = { a: 1 };
with (obj) {
function foo() {
return typeof a;
}
}
alert(foo()); // alerts "number" on Firefox, "undefined" on others
This exposes the object properties to the function scope only on Firefox. On all other browsers that I’ve tested with (IE, Safari, Opera and Chrome), the property isn’t visible inside the function.