<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>js function arguments 传参</title> </head> <body> <script> /*参考:https://www.jianshu.com/p/e6bfa4bdf718 知识点: 1.每一个函数都有一个 arguments 对象,它包含了调用该函数时,传递的所有参数。*/ function max() { /*查找参数中的最大值,并返回。*/ // console.log("arguments:", arguments); // arguments: Arguments(4) [1, 123, 500, 115, callee: ƒ, Symbol(Symbol.iterator): ƒ] if (arguments.length) { let m = arguments[0]; if (arguments.length === 1) return m; for (let i = 1; i < arguments.length; i++) { if (arguments[i] > m) { m = arguments[i]; } } return m; } } console.log(max(1, 123, 500, 115)); // 500 function sum() { /*求所有参数的和,并返回。*/ let s = 0; for (let i of arguments) { s += i; } return s; } console.log("sum:", sum(1, 123, 500)); // sum: 624 </script> </body> </html>
js function arguments 传参.html
于 2020-03-27 12:51:01 首次发布