It is a Function Expression2
Function Expressions can have names as well; the names of Function Expressions are only in scope in the function body1. (Since ECMAScript 5th edition deprecates arguments.callee
, this is the only simple way to write a recursive "anonymous" function.)
Because it is a Function Expression, the name may not1 introduce a new binding in the outer scope.
Also, all functions are objects in JavaScript. In f(..)
, f
is evaluated before it is "invoked" with (..)
; if f
did not evaluate to a function then an error is thrown. This is why callbacks, which are just functions, can be named by variables and passed as parameters.
Also, check the assumption/assertion about the prototype:
var abc = function def() {}
abc.prototype === abc // -> false
abc.name // -> "def"
1 See Bergi's answer.
2 How to easily tell which is which?
The grammar rules only allow function ..
to be parsed as a Function Declaration when it is a SourceElement although most engines will still [incorrectly] parse a Function Declaration as a Statement. SourceElement productions only occur at the top-level "block" of a Program or the top-level "block" of a Function.
In any case, whenever there is a function ..
that appears in a place that requires an Expression, it will be parsed as a Function Expression. Examples that are all parsed as Function Expressions:
// Can only assign values: Statements do not have values!
var f = function () {}
var g = function gName () {} // see Bergi's answer
// Can only pass values: Statements do not have values!
doIt(function () {}) // callback / "anonymous function"
// IIFE: Immediately-Invoked Function Expression
;(function () {})() // standard IIFE
;(function () {} ()) // alternative standard IIFE
;+function () {} () // curious way to write an IIFE
// basically you can use any unary operator to turn it into an expression not only
// + but also - ! ~ and so on which will modify the return value accordingly
The point is that in every single one of the above cases, function ..
appears at a grammar location requiring an Expression and is thus parsed as a Function Expression. (The semicolons at the start of the lines above avoid "ambiguity" with ASI, which is required when writing in a semicolon-free style like I prefer.)
However. ;function () {} ()
and ;function f () {} ()
are both invalid syntax - why? ;-)