箭头函数与普通函数的差异

一、箭头函数中的this指向

箭头函数的this指向通常有两种情况

  • 如果箭头函数处在一个普通函数之中,那么他的this指向与包裹他的外层函数的this指向一致。
  • 其他情况下箭头函数中的this都指向window
let obj = {
  fn:function(){
      console.log('我是普通函数',this === obj)   // true
      return ()=>{
          console.log('我是箭头函数',this === obj) // true
      }
  }
}
console.log(obj.fn()())

let obj = {
    fn:()=>{
        console.log(this === window);
    }
}
console.log(obj.fn())
// true

二、箭头函数不能当做构造函数,不能用new来创建实例。

三、箭头函数内没有普通函数绑定的arguments对象。

四、使用call、apply、bind无法改变箭头函数的this指向。

你可能感兴趣的:(javascript,前端,vue.js)