传递函数引用时获得粗箭头行为
Getting fat arrow behaviour when passing function references
如果我在 ES6 中传递函数:
methodAcceptsFunction(this.myFunction);
...在 ES6 中是否有一种方法可以避免调用 .bind()
类似于粗箭头行为?
methodAcceptsFunction(() => { this.myFunction(); });
在第一种情况下,this
绑定到函数。但在第二种情况下,this
更符合class风格的编程。
is there a way in ES6 to avoid having to call .bind()
on it similar to the fat arrow behaviour?
不,没有。
similar to the fat arrow behaviour
箭头函数的工作方式是它们的环境根本没有 this
值,因此 this
在父环境中查找。
无法修改现有函数的函数环境特征。
如果我在 ES6 中传递函数:
methodAcceptsFunction(this.myFunction);
...在 ES6 中是否有一种方法可以避免调用 .bind()
类似于粗箭头行为?
methodAcceptsFunction(() => { this.myFunction(); });
在第一种情况下,this
绑定到函数。但在第二种情况下,this
更符合class风格的编程。
is there a way in ES6 to avoid having to call
.bind()
on it similar to the fat arrow behaviour?
不,没有。
similar to the fat arrow behaviour
箭头函数的工作方式是它们的环境根本没有 this
值,因此 this
在父环境中查找。
无法修改现有函数的函数环境特征。