JavaScript ?? ??
?? ??
???????? ??? ???? ??? ??? ????.
function abs(x) {
if (x >= 0) { return x;
} else { return -x;
}
}
?? abs() ?? ??? ??? ????.
function? ??? ?? ???? ?????. abs? ??? ?????. (x) ??? ????? ?? ?? ?????. , ?? ????? ???? ?????. { ... } ??? ??? ?? ????, ?? ?? ????? ??? ?? ?? ?? ????.
?? ?? ?? ???? ??? ? return? ???? ??? ???? ??? ????? ?? ?????. ??? ??? ??? ??? ?? ?? ??? ??? ?? ???? ??? ? ????.
return ?? ??? ?? ?? ? ??? ????? ??? ???? ????.
?????? ??? ???? ??? ??? ??? abs() ??? ???? ?? ????, ???? abs? ??? ???? ???? ? ? ????.
??? ??? ???? ? ?? ??? ??? ????.
var abs = function (x) {
if (x >= 0) { return x;
} else { return -x;
}
};
??? ?? function (x) { ... } ? ?? ????? ?? ??? ????. ??? ? ????? ?? abs? ???? ????, ?? abs? ?? ??? ??? ? ????.
?? ? ??? ??? ?????. ? ?? ????? ?? ??? ?? ?? ?? ?? ;? ???? ?? ?? ?? ???? ???.
??? JavaScript ??? ?? ? ?????. ?? ??? ??? ????.
function functionName(arg0, arg1, ...) {
?
}
?? ??
?? ?? ??? ?? ??
?? ??? ??? ??? ?? ??? ???.
?? ?? ??? ?? ?? ???. arg0? arg1? ??? ????? ","? ?????. ?? ???? ?? 0~25?? ? ????. 0? ????? ??? ?????. ????? ?? ?? () ??? ??? ? ????. 25?? ???? ????? JavaScript?? ?????
{}? ??
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(php.cn)</title> <script type="text/javascript"> function hello(name){ document.write((name + ",你好!")); } </script> </head> <body> <input type="button" onclick="hello('小明')" value="確定"/> </body> </html>
?? ???
JavaScript ??? ???? ?? ??? ? ????.
?? ???? ??? ??? ? ????:
var x = function (a, b) {return a * b};
?? ???? ??? ??? ? ??? ??? ??? ?? ????:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(php.cn)</title> </head> <body> <p id="demo"></p> <script> var x = function (a, b) {return a * b}; document.getElementById("demo").innerHTML = x(6, 9); </script> </body> </html>
?? ??(????)
?? ?????? ??? ?? "????"? ?? ?????.
????? ?? ??? ??? ????? JavaScript? ?? ?????.
????? ?? ??? ?? ??? ?????.
??? ??? ???? ?? ??? ? ????.
myFunction(5);
function myFunction(y) {
Return y * y;
}
??? ???? ??? ??? ?? ????? ? ????. ??.
arguments
JavaScript?? ?? ???? ???? ?? ?? ??? ???? ??? ?? ?? ??? ???? ?? ??? ??? ????.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(php.cn)</title> <script type="text/javascript"> function foo(x) { alert(x); // 10 for (var i=0; i<arguments.length; i++) { alert(arguments[i]); // 10, 20, 30 } } foo(10, 20, 30); </script> </head> <body> </body> </html>
Function? ?????
JavaScript?? typeof ???? ???? ?? ??? ???? "function"? ?????.
??? JavaScript ??? ??? ? ???? ?????.
JavaScript ???? ??? ???? ????.
arguments.length ??? ?? ?? ?? ??? ???? ?? ?????.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(php.cn)</title> </head> <body> <p id="demo"></p> <script> function myFunction(a, b) { return arguments.length; } document.getElementById("demo").innerHTML = myFunction(4,7,3); </script> </body> </html>