I'm learning javaScript through the book" Professional JavaScript for Web developers 3rd Edition". And In section 7.1 of Chapter 7, the author puts an example to realize the decoupling between executing function and the function name:
var factorial = (function f(num) {
if (num <= 1) {
return 1;
} else {
return num * f(num - 1);
}
});
The code above does well whether in strict mode or not. As I saw before, the code worked in two steps:
First: declare the function f
function f(num) {
if (num <= 1) {
return 1;
} else {
return num * f(num - 1);
}
}
Second: assign the function name f to the variable factorial
var factorial = f;
So I thought it's feasible to call the function f directly in the example below.
var factorial = (function f(num) {
if (num <= 1) {
return 1;
} else {
return num * f(num - 1);
}
});
f(5);
But in fact, it doesn't work. The console said "f is not a function"; Why? Is there any thing to do with closure?