arrow function and bind

Can you bind arrow functions?

https://stackoverflow.com/questions/33308121/can-you-bind-arrow-functionsjavascript

 

 

You cannot "rebind" an arrow function. It will always be called with the context in which it was defined. Just use a normal function.java

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.post

 

箭頭函數中的this與詞法域綁定, 即便使用bind企圖改變this,也是徒勞的。

 

To be complete, you can re-bind arrow functions, you just can't change the meaning of this.this

bind still has value for function arguments:lua

((a, b, c) => {
  console.info(a, b, c) // 1, 2, 3
}).bind(undefined, 1, 2, 3)()

Try it here: http://jsbin.com/motihanopi/edit?js,consolecode


箭頭函數this肯定, 普通函數中的this,由調用場景/對象肯定。

若是也想模擬arrow函數效果, 能夠使用bind接口,在定義的時候, 就把this綁定到詞法中的this:

function (){orm

}.bind(this)對象

 

箭頭函數不應使用場景

https://dmitripavlutin.com/when-not-to-use-arrow-functions-in-javascript/接口

Defining methods on an object

Callback functions with dynamic context

Invoking constructors

Too short syntax

 

箭頭函數該使用場景

函數短小

函數式計算的入參

須要肯定this爲詞法域的

https://www.imooc.com/article/20607

相關文章
相關標籤/搜索