批改状态:合格
老师批语:
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>函数参数与返回值实例</title></head><body><script>let f=(a,b)=>a+b;//正常返回console.log(f(10,20));//返回NaN:意思是没有一个数值console.log(f(10));// 1.参数不足的解决办法:默认参数f = (a, b = 0) => a + b;console.log(f(10,5));// 2.参数过多的解决办法:...剩余参数f = (a, b) => a + b;console.log(f(1,2,3,4,5));// 如何将全部参数接收到?剩余参数...// ...rest:用在函数的形参中,归并f = (a, b, ...c) =>console.log(a,b,c);console.log(f(1,2,3,4,5));let arr= [1,2,3,4,5];console.log(...arr);console.log(f(...arr));// ...用在函数调用时的实参中是解包f=(...arr)=>arr.reduce((a,c)=>a+c);console.log(f(1,2,3,4,5,6,7,8,9,10))// 返回值:函数只能有一个返回值,默认单值返回// 需要返回多个值怎么办function fn () {return [1,2,3]}// 简化let fn=()=>[1,2,3];let res=fn();console.log(res);fn = () => ({id:2,name:'admin',age:28});res=fn();console.log();</script></body></html>
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号