JavaScript 函数可以通过其参数列表接收多个参数,按位置、名称、可变参数或默认参数传递。
JavaScript 接收多个参数
如何接收多个参数?
JavaScript 函数可以通过其参数列表接收多个参数。参数列表括在函数名后面的圆括号中,由逗号分隔。
展开回答:
1. 通过位置
function sum(a, b) { return a + b; } console.log(sum(1, 2)); // 输出: 3
2. 通过名称
function greet(name, message) { console.log(`${message}, ${name}!`); } greet("John", "Hello"); // 输出: "Hello, John!"
3. 可变参数
function sumAll(...numbers) { let total = 0; for (let num of numbers) { total += num; } return total; } console.log(sumAll(1, 2, 3, 4, 5)); // 输出: 15
4. 默认参数
function greet(name = "World") { console.log(`Hello, ${name}!`); } greet(); // 输出: "Hello, World!" greet("John"); // 输出: "Hello, John!"
注意:
以上就是js如何接收多个参数的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号