Blogger Information
Blog 12
fans 0
comment 0
visits 6066
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
函数参数与返回值的学习实例小结
天空
Original
473 people have browsed it

函数参数与返回值实例

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>函数参数与返回值实例</title>
  8. </head>
  9. <body>
  10. <script>
  11. let f=(a,b)=>a+b;
  12. //正常返回
  13. console.log(f(10,20));
  14. //返回NaN:意思是没有一个数值
  15. console.log(f(10));
  16. // 1.参数不足的解决办法:默认参数
  17. f = (a, b = 0) => a + b;
  18. console.log(f(10,5));
  19. // 2.参数过多的解决办法:...剩余参数
  20. f = (a, b) => a + b;
  21. console.log(f(1,2,3,4,5));
  22. // 如何将全部参数接收到?剩余参数...
  23. // ...rest:用在函数的形参中,归并
  24. f = (a, b, ...c) =>console.log(a,b,c);
  25. console.log(f(1,2,3,4,5));
  26. let arr= [1,2,3,4,5];
  27. console.log(...arr);
  28. console.log(f(...arr));
  29. // ...用在函数调用时的实参中是解包
  30. f=(...arr)=>arr.reduce((a,c)=>a+c);
  31. console.log(f(1,2,3,4,5,6,7,8,9,10))
  32. // 返回值:函数只能有一个返回值,默认单值返回
  33. // 需要返回多个值怎么办
  34. function fn () {
  35. return [1,2,3]
  36. }
  37. // 简化
  38. let fn=()=>[1,2,3];
  39. let res=fn();
  40. console.log(res);
  41. fn = () => ({
  42. id:2,
  43. name:'admin',
  44. age:28
  45. });
  46. res=fn();
  47. console.log();
  48. </script>
  49. </body>
  50. </html>
Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post