Skip to content

小心你的箭头函数 #11

Open
Open

Description

@imondo

使用箭头函数你要格外的小心;箭头函数运行时候bind this 。

场景:

  • 定义对象方法

  • 定义原型方法

  • 定义构造函数

  • 定义事件回调函数

箭头函数没有定义this绑定

var obj = {
  i: 10,
  b: () => console.log(this.i, this),
  c: function() {
    console.log( this.i, this)
  }
}
obj.b(); 
// undefined, Window{...}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions