Edit: it’s NOT an assignment of a function declaration to a named variable – check the accepted answer. Leaving title as it is because other people might make the same error as me.
While reading Paul Irish’s infinitescroll jquery plugin code, I stumbled again and again over the following pattern:
...
_create : function infscr_create (options, callback) { /* ... */ },
...
What is the benefit of doing this instead of:
...
_create : function (options, callback) { /* ... */ },
...
The benefit of that (which is called a “named function expression”) is that the function has an actual name. In your second version, the property has a name, but the function doesn’t. Giving functions actual names helps your tools help you (call stack listings, breakpoint listings, etc.) More: Anonymouses anonymous
The disadvantage to it is that it has unexpected results in some broken JavaScript engines, like the one in IE8 and earlier. In IE8 and earlier, Paul Irish’s version creates two separate functions at two completely different times. But it’s not really a problem unless you keep and use references to both of them, and expect them to be the same function (for instance, when hooking up and unhooking event handlers). Given it’s Paul, I’m guessing he’s being sure not to do that.
Re your question title: Note that it’s not a function declaration, but you can be forgiven for thinking it is, as it looks almost exactly like one. 🙂 It’s a function expression. Function declarations and function expressions happen at completely different times, and have different impacts on the scope in which they’re created.
Just for completeness: