This article mainly introduces some basic knowledge about Function Definition in javascript, including Function declaration, Function expressions, and Function constructor. It is very simple and practical, if you have any need, refer.
Function declarative
Function funname (parameter) {code executed}
The declarative function will not be executed immediately. It will be executed only after we call it: funname ();
* A semicolon is used to separate executable JavaScript statements. function declaration is not an executable statement, so it is not ended with a semicolon.
Function expression
Var x = function (parameter) {code block executed };
The function defined by the function expression is actually an anonymous function (this function has no name and is directly stored in the variable)
* The function expression ends with a plus sign because it is an execution statement.
Function Constructor
The Code is as follows:
Var myFunction = new Function ("a", "B", "return a * B ");
Call the function and assign it to a variable:
The Code is as follows:
Var x = myFunction (4, 3); // x = 12;
In actual production, it is not recommended to use constructors to define functions. The above example can be rewritten:
The Code is as follows:
Var myFunction = function (a, B) {return a * B };
Var x = myFunction (4, 3); // x = 12;
The above is all the content of this article. I hope you will like it.