ES6中对箭头函数的使用

箭头函数

1.this的指向

使用箭头函数 箭头函数没有自己的this值,他的this是指向父级作用域。被调用时this也不用跟着改变。
  const Linxi = {
            name: 'Linxi',
            hobbies: ['Coding', 'Sleeping', 'Reading'],
            printHobbies: function () {
                // var self=this;
                this.hobbies.map(hobby => {
                    console.log(`${self.name} loves ${hobby}`);
                })
            }
        }
        Linxi.printHobbies();

2.arguments

箭头函数里面没有arguments
 let show=()=>{
            console.log(arguments);  //报错
      }
  show(1,2,3,4);

3.扩展运算符

        let show = (...args) => {
            console.log(args);
        }
        show(1, 2, 3, 4);

猜你喜欢

转载自www.cnblogs.com/linxim/p/11768561.html