跳至主要內容

05.箭头函数(ES6)

pinia小于 1 分钟

简版

函数实干什么用的?

  • 对象{数据},

  • 函数{流程}:是对函数的封装

箭头函数(最纯粹的函数)

const abc = () => console.log('abc')
abc()

箭头函数的特点

  1. 没有this,super,argument
  2. 不能使用this,箭头函数的this是根据作用域链访问的
  3. 不能使用new调用
  4. 没有原型
  const obj = {
    a:1,
    fun:function () {
      setTimeout(function () {
        console.log(this.a)
      },1000)
    }
  }
  obj.fun() // undefined
  const obj2 = {
    a:1,
    fun:function () {
      //this指向obj2,所以可以用变量保存this,闭包
      variable = this
      setTimeout(() => {
        console.log(variable.a)
      },1000)
    }
  }
  obj2.fun() // 1
  const obj3 = {
    a:1,
    fun: function (){
      setTimeout(() => {
        console.log(this.a)
      },1000)
    }
  }
  obj3.fun() // 1
上次编辑于:
贡献者: 林深不见鹿