JavaScript function definition
JavaScript uses the keyword function to define functions.
A function can be defined through a declaration or an expression.
Function declaration
In the previous tutorial, you already learned the syntax of function declaration:
function functionName(parameters) {
Executed code
}
The function will not be executed immediately after it is declared, but will be called when we need it.
Example
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <p>本例调用的函数会执行一个计算,然后返回结果:</p> <p id="demo"></p> <script> function myFunction(a,b){ return a*b; } document.getElementById("demo").innerHTML=myFunction(4,3); </script> </body> </html>
Run the program and try it
Tips: The semicolon is Used to separate executable JavaScript statements. Since the function declaration is not an executable statement, it does not end with a semicolon.
Function expression
JavaScript functions can be defined by an expression.
Function expressions can be stored in variables:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <p>函数可以存储在变量中:</p> <p id="demo"></p> <script> var x = function (a, b) {return a * b}; document.getElementById("demo").innerHTML = x; </script> </body> </html>
Run the program to try it
After the function expression is stored in the variable, the variable can also be used as a function Use:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <p>函数存储在变量后,变量可作为函数使用:</p> <p id="demo"></p> <script> var x = function (a, b) {return a * b}; document.getElementById("demo").innerHTML = x(4, 3); </script> </body> </html>
Run the program to try it
The above function is actually an anonymous function (the function has no name).
Function is stored in a variable and does not require a function name. It is usually called through the variable name.
Tip: The above function ends with a semicolon because it is an execution statement.
Function() constructor
In the above example, we learned that functions are defined through the keyword function.
Functions can also be defined through the built-in JavaScript function constructor (Function()).
Example
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <p>JavaScrip 内置构造函数。</p> <p id="demo"></p> <script> var myFunction = new Function("a", "b", "return a * b"); document.getElementById("demo").innerHTML = myFunction(4, 3); </script> </body> </html>
Run the program to try it
Tips: In JavaScript, many times , you need to avoid using the new keyword.
Actually, you don't have to use a constructor. The above example can be written as:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <p id="demo"></p> <script> var myFunction = function (a, b) {return a * b} document.getElementById("demo").innerHTML = myFunction(4, 3); </script> </body> </html>
Run the program and try it
Function Hoisting (Hoisting)
We have already learned about "hoisting (hoisting)" in the previous tutorial.
Hoisting is JavaScript’s default behavior of hoisting the current scope to the front.
Hoisting is applied to variable declarations and function declarations.
Therefore, the function can be called before declaration:
myFunction(5);
function myFunction(y) {
return y * y;
}
Cannot be promoted when using an expression to define a function.
Self-calling functions
Function expressions can "self-call".
Self-calling expressions will be called automatically.
If the expression is followed by (), it will be called automatically.
The declared function cannot be called by itself.
By adding parentheses, it is shown that it is a function expression:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <p>函数可以自动调用:</p> <p id="demo"></p> <script> (function () { document.getElementById("demo").innerHTML = "Hello! 我是自己调用的"; })(); </script> </body> </html>
Run the program to try it
The above function is actually an anonymous self-calling function (no function name).
Function can be used as a value
JavaScript function can be used as a value:
Example
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <p>函数可作为一个值:</p> <p>x = myFunction(4,3) 或 x = 12</p> <p>两种情况下,x 的值都为 12。</p> <p id="demo"></p> <script> function myFunction(a, b) { return a * b; } var x = myFunction(4, 3); document.getElementById("demo").innerHTML = x; </script> </body> </html>
Run the program and try it
JavaScript functions can be used as expressions:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <p>函数可作为一个表达式使用。</p> <p id="demo"></p> <script> function myFunction(a, b) { return a * b; } var x = myFunction(4, 3) * 2; document.getElementById("demo").innerHTML = x; </script> </body> </html>
Run the program and try it
Function is an object
Use the typeof operator in JavaScript to determine the function type will return "function".
But it is more accurate to describe a JavaScript function as an object.
JavaScript functions have properties and methods.
arguments.length property returns the number of parameters received by the function calling process:
Example
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <p> arguments.length 属性返回函数接收到参数的个数:</p> <p id="demo"></p> <script> function myFunction(a, b) { return arguments.length; } document.getElementById("demo").innerHTML = myFunction(4, 3); </script> </body> </html>
Run the program and try it
toString() method returns the function as a string:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <p> toString() 将函数作为一个字符串返回:</p> <p id="demo"></p> <script> function myFunction(a, b) { return a * b; } document.getElementById("demo").innerHTML = myFunction.toString(); </script> </body> </html>
Run the program to try it
Tips: A function defined as an attribute of an object is called an object method. If the function is used to create a new object, it is called the constructor of the object.