This article mainly introduces the function modes in JavaScript, this article describes the syntax, expression, naming expression, declaration, declaration, and expression for creating a function, function upgrade, and real-time function mode, if you need it, you can refer to the JavaScript design pattern to improve code reusability and readability, making it easier to maintain and expand the code.
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.
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;
};
The value assigned to the add variable is the function definition itself. In this way, add becomes a function that can be called anywhere.
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: