15_函数的扩展.html 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. // let foo = () => {
  60. // console.log(this);
  61. // }
  62. // foo();
  63. let obj = {
  64. a:100,
  65. b:"hello",
  66. foo(){
  67. // console.log(this);
  68. let foo2 = () => {
  69. console.log(this);
  70. }
  71. foo2();
  72. function foo3(){
  73. console.log(this);
  74. }
  75. foo3();
  76. }
  77. }
  78. obj.foo();
  79. </script>
  80. </body>
  81. </html>