• ES6__函数的扩展


      1 /**
      2  * 函数的扩展
      3  *  1 为函数参数指定默认值
      4  *  2 函数的 rest 参数
      5  *  3 箭头函数
      6  */
      7 
      8 // ------------------------------------------------
      9 
     10 // function fn(a, b){
     11 //   a = a || 10;
     12 //   b = b || 20;
     13 //   console.log(a + b);
     14 // }
     15 
     16 // fn();
     17 // fn(0, 10);
     18 
     19 // function fn(a = 10, b = 20){
     20 //   console.log(a + b);
     21 // }
     22 // 
     23 // fn();
     24 // fn(0, 10);
     25 
     26 
     27 // ----------------------------------------------
     28 //rest 参数形式为(“...变量名”),用于获取函数的多余参数,这样就不需要使用arguments对象了。rest 参数搭配的变量是一个数组,该变量将多余的参数放入数组中。
     29 
     30 // function sum(){
     31 //   var args = arguments;
     32 //   var res = 0;
     33 //   for(var i=0; i<args.length; i++){
     34 //     res += args[i];
     35 //   }
     36 //   console.log(res);
     37 // }
     38 // 
     39 // sum(1, 2, 3, 4, 5);
     40 //只能在...arr之前添加参数
     41 // function sum(a, ...arr, b){
     42 //   var res = a;
     43 //   for(var i=0; i<arr.length; i++){
     44 //     res += arr[i];
     45 //   }
     46 //   console.log(res);
     47 // }
     48 // 
     49 // sum(10, 1, 2, 3, 4, 5);
     50 
     51 // -----------------------------------------------
     52 // 使用“箭头”(=>)定义函数。
     53 
     54 // const fn = a => a;
     55 // 
     56 // const fn2 = function (a){
     57 //   return a;
     58 // };
     59 // 
     60 // console.log(fn(1));
     61 // console.log(fn2(2));
     62 
     63 // const fn = (a, b) => a + b;
     64 // 
     65 // console.log(fn(1, 2));
     66 
     67 // const fn = (a, b) => {
     68 //   a = a * 2;
     69 //   b = b * 2;
     70 //   return a + b;
     71 // };
     72 // 
     73 // console.log(fn(1, 2));
     74 
     75 // const fn = (a, b) => ({a, b});
     76 // 
     77 // console.log(fn(1, 2));
     78 
     79 // var arr = [5, 2, 3, 4, 1];
     80 
     81 // arr.sort(function (a, b){
     82 //   return a - b;
     83 // });
     84 // 
     85 // console.log(arr);
     86 
     87 // arr.sort((a, b) => a - b);
     88 // 
     89 // console.log(arr);
     90 
     91 // 1 箭头函数体内没有自己的this对象,所以在使用的时候,其内部的this就是定义时所在环境的对象,而不是使用时所在环境的对象。
     92 
     93 // function fn(){
     94 //   setTimeout(function (){
     95 //     console.log(this);
     96 //   }, 1000);
     97 //   setTimeout(() => {
     98 //     console.log(this);
     99 //   },1000);
    100 // }
    101 // 
    102 // var obj = {a: 1};
    103 // 
    104 // fn.call(obj);
    105 
    106 // 不能给箭头函数使用 call apply bind 去改变其内部的this指向
    107 
    108 // 2 箭头函数体内没有arguments对象,如果要用,可以用Rest参数代替。
    109 
    110 // function fn(){
    111 //   setTimeout(() => {
    112 //     console.log(arguments);
    113 //   }, 1000)
    114 // }
    115 // 
    116 // fn(1, 2, 3);
    117 
    118 // const fn = (...arr) => arr;
    119 // 
    120 // console.log(fn(1, 2, 3, 4));
    121 
    122 // 3 不可以当作构造函数,不可以使用new命令,否则会抛出一个错误。
    123 
    124 // const Fn = (a, b) => a + b;
    125 
    126 // const f = new Fn(1, 2);  报错
    127 
    128 
    129 //4.箭头函数不能用Generator函数。
  • 相关阅读:
    [ZZ]风险驱动的测试
    移动测试书籍推荐
    4月收藏
    Appium路线图及1.0正式版发布
    匿名吐槽和测试小道消息
    文章收藏
    [ZZ]最小化不可重现的bug
    华人世界——客家足迹行
    移动测试会第七期
    2月收藏
  • 原文地址:https://www.cnblogs.com/xiaozhishang/p/8759670.html
Copyright © 2020-2023  润新知