This article gives a detailed explanation of the four functional modes of javascript and attaches examples to help you understand and apply them. In javascript, a function is a type of object, which means that it can be passed to other functions as a parameter. In addition, a function can also provide a scope.
Basic javascript Functions: javascript learning notes (4) function
Syntax for creating a function
Name function expression
The Code is as follows:
// Name function expression
Var add = function add (a, B ){
Return a + B;
};
Function expression
The Code is as follows:
// Also known as anonymous Functions
Var add = function (a, B ){
Return a + B;
};
Function Declaration
The Code is as follows:
Function foo (){
// Code here
} // You do not need a semicolon here
In the trailing semicolon, the function expression should always use the semicolon, and the declaration of the function does not need to end with the semicolon.
Function declaration and Expression
Hoisting)
The behavior of the function declaration is not equivalent to the name function expression. The difference is that the hoisting behavior. See the following example:
The Code is as follows: