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

相关文章
相关标签/搜索