This article mainly introduces JavaScript some basic knowledge of function definition, including function declaration, function expression and function constructor, etc., very simple and practical, the need for small partners can refer to.
Function-declaration
?
1 2 3 4 5 |
function Funname (parameter) {... Executed code} |
Declarative functions do not execute immediately, and require our invocation to execute: funname ();
* Semicolons are used to separate executable javascript statements, because function declarations are not an executable statement, so do not end with semicolons.
function expression
?
1 2 3 4 5 |
var x = function (parameter) {... code block to execute}; |
A function that is defined by a function expression is actually an anonymous function (this function has no name and is stored directly in the variable)
* A function expression ends with a semicolon, because it is an execution statement.
Function constructor
The code is as follows:
var myfunction = new Function ("A", "B", "return A * b");
Call a function and assign it to a variable:
The code is as follows:
var x = MyFunction (4, 3); x = 12;
It is not recommended to use constructors to define functions in actual production, and the above examples can be rewritten as:
The code is as follows:
var myfunction = function (a,b) {return a * b};
var x = MyFunction (4, 3); x = 12;
The above mentioned is the entire content of this article, I hope you can enjoy.