29 ms·
Can someone please explain to me in a bit more detail what the advantage of not having named functions is?
by dyladan 13y ago
Can someone please explain to me in a bit more detail what the advantage of not having named functions is?
- brianchu 13y agoThere's a difference between assigning an anonymous function to a variable and declaring a named function. It's the difference between: var funcName = function(){} function funcName(){} The problem is that using named functions involves nuances that can be confusing. Generally it's considered idiomatic to just avoid named functions altogether. You can read about some of the nuances here: http://kangax.github.io/nfe/ http://kangax.github.io/nfe/
- gadr90 13y agoNice material! Thanks for sharing.
- secoif 13y ago> Generally it's considered idiomatic to just avoid named functions altogether. This is not idiomatic. Many developers only use named functions. The obvious reason is they make stack traces far more readable, though you can get around this by naming functions anyway: var myFunction = function myFunction() {} The hoisting 'nuances' you decry are a boon. Hoisting means you never have to worry about the order functions are declared – especially useful in cases where your functions call each other. You also get another avenue to declutter your code by pushing any crufty utility functions to the bottom of the page.
- lowboy 13y agoNamed function expressions will cause memory leaks in IE < 9. Shouldn't be a problem forever, but some devs now might be impacted. Pushing utility functions to the bottom of the current file is another reason to use a modular js architecture.
- function_seven 13y agoI read your link and found it to be very educational, but I don't believe it supports avoiding named function declarations, but rather named function expressions, since those are the source of inconsistencies between browsers. My takeaway is that it is wise to avoid using named expressions, but fine to stick with anonymous function assignments, or declared functions. To add a third example to your list: var funcName = function(){}; function funcName(){} var funcName = function funcName(){}; // <-- Danger!
- deleted 13y ago[deleted]
- mck- 13y agoSee http://stackoverflow.com/questions/336859/var-functionname-function-vs-function-functionname http://stackoverflow.com/questions/336859/var-functionname-f...
- deleted 13y ago[deleted]
- joev_ 13y agoThere is no advantage. In fact, it's more annoying when you're debugging, since you don't get the fn name in the trace.