If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Enjoy an ad free experience by logging in. Not a member yet? Register.

The only difference in this case is that you can re-use the named function while the anonymous one would have to be redefined everytime (in which case the performance would suffer). However, which way to go depends on the application.

Apparently, the answer depends on the browser. Although perhaps it shouldn't. The ECMAScript standard says that all identical functions can be (and should be, though the "should" is less clear) represented by a single function that is shared.

And some browsers apparently do exactly that. Meaning that, unless you used your function myfunc in some place *OTHER* than shown here, the two sets of code you showed are identical. All those supposedly separate anonymous functions will actually all be automatically rolled into one implementation by the newer and smarter browsers.

However, it does seem true that there are enough older browsers out there that do *NOT* do this that the second approach is the better one in the near future.

Yes, there are some browsers that don't understand named functions in assignments and so those browsers will fail to run the code.

It is best to avoid using named functions at all. Assigning anonymous functions to variables allows for far greater flexibility and can be used in many places where named functions are not permitted (such as inside an if statement).

no, it's still a function expression instead of a lexical-hoisting function statement. the only place somename could be seen in a your function expression is inside the function.

named functions are easier to debug than digging through 50 different anonymous() calls in a exception stack or firebug trace.

i think for that reason alone, it's better to use named function statements.

how to spot the diff?
if the char to the left of the word function is a line-terminator or semi-colon, and there is a white-space after "function" followed by /[a-z_$]+/i before the opening paren, it's a statement.

almost always, the chars to the left of the word function is whitespace and/or "=" in function expressions, which may or may not have an internal name.

i recommend giving even function expressions names for self-documentation, easier maintenance, and more-precise debugging information when you need it most.

Last edited by rnd me; 01-07-2013 at 10:39 PM.

Create, Share, and Debug HTML pages and snippets with a cool new web app I helped create: pagedemos.com

i don't see how a name or lack of name makes any difference at all to a function expression in any browser i've ever used. such a browser would not meet the spec.

can you give an example?

Three examples:

Named functions cannot be used in expressions in IE8. You can either use a named function outside an expression or an anonymous function inside an expression in that browser but cannot give the function a name and assign it in the same statement and have it run in that browser. IE8 runs jScript rather than JavaScript and so isn't required to comply with the JavaScript spec as it complies with the jScript spec which doesn't allow named functions in expressions.

Named functions cannot be used in expressions in IE8. You can either use a named function outside an expression or an anonymous function inside an expression in that browser but cannot give the function a name and assign it in the same statement and have it run in that browser. IE8 runs jScript rather than JavaScript and so isn't required to comply with the JavaScript spec as it complies with the jScript spec which doesn't allow named functions in expressions.

maybe i'm misunderstanding your claims.
if you can show code describing what you say won't work i'd be very interested.
i do think that basic IE8 jscript should not be walled-out by a fundamental pattern, at least not just yet.
still, i can see no evidence to support your claim, so i must shift the burden of demonstrativeness to you.

Last edited by rnd me; 01-08-2013 at 08:12 AM.

Create, Share, and Debug HTML pages and snippets with a cool new web app I helped create: pagedemos.com