说明:代码取自网络,注释为笔者学习时添加
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>arguments应用,求参数总和</title> <style> pre{ color:green; padding:10px 15px; background:#f0f0f0;/*灰白色*/ border:1px solid #333;/*黑色*/ font:12px/1.5 Courier New;/*行高18*/ } span{ color:#999;/*浅黑色*/ } </style> </head> <body> <pre> <script type="text/javascript"> var i = iResult = 0 function sum() { for (var i = 0; i < arguments.length; i++) { iResult += arguments[i] } return iResult } <span>//应用</span> alert(sum(1,2,3,4,5,6,7,8,9,10)) <span>//输出55</span> </script> </pre> <script> var i = iResult = 0; function sum() { for (var i = 0; i < arguments.length; i++) { iResult += arguments[i]; } return iResult; } alert(sum(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); </script> </body> </html>