There are three methods: 1. function keyword, syntax "function function name (parameter list) {//declaration}"; 2. Use function expression form "var variable name = function (parameter list) {//Declaration}" to define; 3. Use the "new Function()" constructor to define.
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
Composition of function: function name function body
1. Use the function keyword to define the function--has priority Level, the function defined by the function keyword will be executed first
##
function functionName(arg0, arg1 ,..., argN){ statements }
2. Define the function in the form of a function expression (that is, copy the anonymous function to the variable)
var variable = function(arg0, arg1 ,..., argN){ statements } console.log(typeof variable); //function
3. Use the new Function constructor to define the function
var variable = new Function('name','alert("hello,"+name)'); //最末尾的是函数体,其前面的都是参数 console.log(typeof variable); //function
Note:
(1) Use fucntion key For word-defined functions, once the function is declared, it is allowed to be called at any time (before the function is defined, after the function is defined, or inside the function, it can be called at any location) (2) Use function expressions and new Function constructor definitions Functions cannot be used before the function is defined. Parameters of the function: Formal parameters: Parameters taken when the function is defined Actual parameters: Parameters taken when the function is calledFor more programming-related knowledge, please visit:Programming Video! !
The above is the detailed content of There are several ways to define functions in JavaScript. For more information, please follow other related articles on the PHP Chinese website!