javascript 函數javascript
return 後面沒有返回值html
表明提前退出語句,return後面的語句都再也不執行java
此時返回值爲undefined數組
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <script> function myFun(arg){ //非數值,不進行操做; //數值,返回數值的2倍 if(isNaN(arg)) return; return arg*2; } console.log(myFun('a'));//undefined console.log(myFun(2));//4 </script> </body> </html>
arguments 類數組函數
使用[]訪問參數元素,索引是從0開始的整數spa
使用length獲取參數個數code
非嚴格模式下,能夠經過arguments修改元素的值htm
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <script> function myFun(){ console.log(arguments.length); console.log(arguments[0]); console.log(arguments[1]); } myFun(3,5);//2 3 5 function myFun2(num){ arguments[0]=10; console.log(num); } myFun2(3);//10 </script> </body> </html>
求任意一組數的平均值blog
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <script> function getAvg(){ var sum=0, len=arguments.length; for(var i=0;i<len;i++){ sum+=arguments[i]; } console.log(sum/len); } getAvg(3,5,9,11);//7 </script> </body> </html>