1. Function declaration
The function declaration begins with the function keyword, followed by the required functions (variable) name and an optional comma-delimited list of arguments, followed by the body of the function enclosed in curly braces. The function declaration must be a separate JavaScript statement.
2. Function expressions
In any case, a function that is part of another JavaScript statement (such as copying the right side of an expression equals the argument of a function) is called a function expression.
3. Compare
function declaration
function Myfunctiondeclaration () { innerfunction () {}}//) The following is a functional expression var myFunc = function ( {};myfunc () {function () {return function () {};});( function Namedfunctionexpression () {}) (); +function () {} (); -function () {} ();! function () {} (); ~function () {} ();
Myfunctiondeclaration is a function declaration that contains other function declarations (innerfunction)
function expressions are always part of other JavaScript statements, such as the right side of the variable declaration equals sign:
var myFunc = function () {};
or the parameters of other functions:
MyFunc (function () {
return function () {};
Or immediately execute the function:
(function Namedfunctionexpression () {}) ();
Or be decorated with an operator:
+function () {} ();
There is a difference between a function declaration and a function expression, in addition to where the code is placed, that is, the function declaration must have a function name, and the function name of the function expression can be omitted.
The function declaration must have a function name because the function is called the basic requirement, we must be able to reference it when calling a function, and the only way is through the function name.
function expressions are part of other JavaScript statements, so we have other ways to reference them, such as a function being assigned to a variable, which can be used to access a variable name:
var doNothing = function () {};d onothing ();
Or as an argument to another function, it can be accessed by the name of the parameter:
function dosomething (action) { action (); }
About executing functions immediately:
The immediate execution function must be wrapped in parentheses for the simple reason that the JavaScript parser needs to distinguish between function declarations and function expressions, omitting parentheses outside the function, and calling directly (function () {} (2) without parentheses outside the function).
The JavaScript parser starts parsing and ends execution because the statement starts with the Function keyword and is declared as a functional, and because the function declaration must have a function name, an exception is thrown. The parentheses outside the function give the JavaScript parser a signal for a function expression. In order to achieve the same purpose, you can also write:
(function (a) {} (3)), the parentheses function the same as above.
The function of the bitwise operator is the same as above.
The difference between a function declaration and a function expression