Functions are data
Functions in JavaScript are actually data. This is an important concept that we'll need later on. This means that you can create a function and assign it to a variable:
var f = function () { return 1; };
This way of defining a function is sometimes referred to as function literal notation.
The part function () { return 1; }
is a
function expression. A function expression can optionally have a name, in which case it becomes a named function expression
(NFE). So, this is also allowed, although rarely seen in practice (and causes IE to mistakenly create two variables in the enclosing scope: f
and myFunc
):
var f = function myFunc() { return 1; };
As you can see, there's no difference between a named function expression and a function ...
Get JavaScript : Object-Oriented Programming now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.