<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script>
// 形参:创建函数时声明的参数。
// 实参:调用函数时(实际)传入的参数。
// JS 中的函数不限制参数的数量,也不限制参数的类型。
function foo (x, y, z) {
// 函数体内有一个 arguments 类似于数组的对象,对象中保存着所有传入的实参
console.log(arguments)
console.log(arguments.length) // 实参的数量
console.log(foo.length) // 形参的数量
if (x > y) {
return x;
} else {
return y;
}
}
// JS 中的函数不限制参数的数量
// console.log(foo(12))
console.log(foo(12, 34, 56, 67))
// 也不限制参数的类型
// console.log(foo('abc', true))
// 形参的作用:能够标识函数所需参数的数量
function bar () {
if (arguments[0] > arguments[1]) {
return arguments[0];
} else {
return arguments[1];
}
}
// 形参的作用:能够明确地表示每个参数的意义
function fullName (firstName, lastName) {
return firstName + lastName;
// return arguments[0] + arguments[1];
}
// console.log(fullName('马什么', '梅'))
// arguments 对象的使用场景:求任意数量的数字的和
function sum () {
var result = 0;
for(var i = 0, len = arguments.length; i < len; i++) {
result += arguments[i]
}
return result;
}
// console.log(sum(1, 2, 3 , 4, 5))
</script>
</body>
</html>
来源:CSDN
作者:上头upup
链接:https://blog.csdn.net/qq_45802159/article/details/103246077