15_函数的扩展.html 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. </head>
  8. <body>
  9. <script>
  10. // function foo(a,b){
  11. // if(a && b){
  12. // console.log(a+b);
  13. // }else{
  14. // console.log("缺少参数");
  15. // }
  16. // }
  17. // foo(1)
  18. // ES6 新增默认值 如果参数有值 就用参数的值 如果没有值 就用默认值
  19. // function foo(a=0,b=0){
  20. // console.log(a+b);
  21. // }
  22. // foo(1,2)
  23. // function foo(a,b,c = 0){
  24. // console.log(a+b+c);
  25. // }
  26. // foo(1,2,3)
  27. // 函数length 属性 代表形参的个数 且没有默认值
  28. // console.log(foo.length);
  29. // function foo(){};
  30. // let foo = function(){};
  31. // ES6 新增箭头函数 语法更加简洁
  32. // let foo = () => {};
  33. // let foo = (a,b) => {
  34. // console.log(a+b);
  35. // }
  36. // foo(1,2);
  37. // 当箭头函数省略大括号时 会默认返回值 返回箭头后边的第一条语句;
  38. // let foo = (a,b) => a+b;
  39. // console.log(foo(1,2));
  40. // function foo(a,b){
  41. // return a+b;
  42. // }
  43. // console.log(foo(1,2));
  44. // function foo(...arg){
  45. // console.log(arg);
  46. // }
  47. // foo(1,2,3);
  48. // function foo(){
  49. // console.log(arguments[0]);
  50. // }
  51. // foo(1,2,3,4,5)
  52. // let foo = () => {
  53. // console.log(arguments);
  54. // }
  55. let foo = (...arg) => {
  56. console.log(arg);
  57. }
  58. foo(1,2,3,4,5);
  59. </script>
  60. </body>
  61. </html>