Ran across a somewhat unfortunate glitch in IE's handling of JavaScript recently. I've wanted to get in the habit of naming my function expressions as of late. The sole reason being so that when I have to debug, the call stack will actually show something useful rather than just "Anonymous Function" all the way down. Regarding function expression names, this is what the ECMA-262 docs state in 13:
The Identifier in a FunctionExpression can be referenced from inside the FunctionExpression's FunctionBody to allow the function to call itself recursively. However, unlike in a FunctionDeclaration, the Identifier in a FunctionExpression cannot be referenced from and does not affect the scope enclosing the FunctionExpression.So, given the following code snippet:
function someClass(){} someClass.prototype = {}; someClass.prototype.someMethod = function someMethod(){ alert('Hi'); }; someMethod();We should receive an error as someMethod should not be defined as a global function. This is true for at least Chrome and Firefox. IE8 on the other hand, will alert 'Hi'. Older versions appear to show the same behavior. This isn't a huge deal but unfortunately it does result in named function expressions polluting the global namespace. As a result, I have stopped naming them for now. Though it probably wouldn't cause two many issues, there is still a chance of some headaches down the line when someone calls a global function and something else gets ran.
Comments