DRAFT
FunctionsCannotBeImmediatelyInvoked
Misconception:
A function can be called only if it was previously stored in a variable.
Incorrect
Functions cannot be called in the expression in which they are defined
Correct
Functions can be immediately called in the expression in which they are defined
CorrectionHere is what's right.
Here is what's right.
There are two ways to define a function:
through a function declaration:
function foo(){}; // function declaration () => {} // arrow function declaration
through a function expression:
// function expressions (function foo(){}); const foo = function(){}; const bar = { foo: function(){} } // arrow function expressions (() => {}); const foo = () => {}; const bar = { foo: () => {} }
Function declarations cannot be immidiately invoked:
Function expressions can be invoked as they are defined:
Difficult cases
Internal scope
Function expressions can be named, meaning that it is possible to access the function from within its own scope:
Function expressions are not function declarations
A function expression does not declare the function in the containing scope of the expression.
Language
JavaScript