JavaScript ?? ??
JavaScript? ??? ???? ?? ??? function? ?????.
??? ???? ??? ?? ?? ???? ?? ????.
?? ??
?? ?????? ?? ??? ??? ?????.
function functionName(parameters) {
??? ??
}
?? ?? ??? ?? ????? ??? ??? ? ?????.
Example
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(php.cn)</title> </head> <body> <p>本例調用的函數會執(zhí)行一個計算,然后返回結果:</p> <p id="demo"></p> <script> function myFunction(a,b){ return a*b; } document.getElementById("demo").innerHTML=myFunction(4,3); </script> </body> </html>
????? ???? ??? ???
Tips: ????? ?? ??? JavaScript ?? ???? ? ?????. ?? ??? ?? ??? ?? ??? ??? ?????? ??? ????. .
?? ???
JavaScript ??? ????? ??? ? ????.
?? ???? ??? ??? ? ????:
<!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>
????? ???? ??? ???
?? ???? ??? ??? ? ??? ??? ??? ?? ????.
<!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>
????? ???? ??? ???. it
? ??? ?? ?? ?????(???? ??? ????).
??? ??? ???? ?? ??? ???? ??? ????? ?? ??? ?? ?????.
Tip: ? ??? ????? ??? ?????? ????. ?
Function() ???
?? ??? ??? function ???? ?? ????? ?? ?????.
??? ??? JavaScript ?? ???(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>
????? ???? ??? ???
Tip: JavaScript??? new ???? ???? ??? ? ??? ????.
??? ???? ??? ??? ????. ?? ?? ??? ?? ??? ? ????.
<!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>
????? ???? ??? ???
?? ????(Hoisting)
?? ?????? ??? ?? "????(hoisting)"? ?? ?????.
????? ?? ??? ??? ????? JavaScript? ?? ?????.
????? ?? ??? ?? ??? ?????.
??? ??? ???? ?? ??? ? ????.
myFunction(5);
function myFunction(y) {
Return y * y;
}
??? ???? ??? ??? ?? ????? ? ????. ??.
?? ?? ??
?? ???? "?? ??"? ?????.
?? ?? ??? ???? ?????.
??? ?? ()? ?? ???? ?????.
??? ??? ?? ??? ? ????.
??? ???? ?? ????? ?????.
<!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>
????? ???? ??? ???
? ??? ??? ??? ?? ?? ?????(?? ?? ??).
??? ??? ??? ? ??
JavaScript ??? ??? ??? ? ??:
?
<!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>
????? ???? ??? ???.
JavaScript ???
<!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>
????? ???? ??? ???
Function? ?????
JavaScript?? typeof ???? ???? ?? ??? ???? "function"? ?????.
??? JavaScript ??? ??? ? ???? ?????.
JavaScript ???? ??? ???? ????.
arguments.length ??? ?? ?? ?? ?? ???? ?? ?????.
Instance
<!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>
????? ???? ??? ???.
toString() ???? ??? ???? ?????. :
<!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>
????? ???? ?? ??? ???
Tips: ??? ???? ??? ??? ?? ????? ???. ?? ??? ???? ? ??? ???? ?? ?? ????? ???. ??.