Can you bind 'this' in an arrow function?

前端 未结 11 2461
囚心锁ツ
囚心锁ツ 2020-11-22 03:09

I\'ve been experimenting with ES6 for a while now, and I\'ve just come to a slight problem.

I really like using arrow functions, and whenever I can, I use them.

11条回答
  •  野性不改
    2020-11-22 03:35

    You cannot rebind this in an arrow function. It will always be defined as the context in which it was defined. If you require this to be meaningful you should use a normal function.

    From the ECMAScript 2015 Spec:

    Any reference to arguments, super, this, or new.target within an ArrowFunction must resolve to a binding in a lexically enclosing environment. Typically this will be the Function Environment of an immediately enclosing function.

提交回复
热议问题