【发布时间】:2017-09-21 21:29:00
【问题描述】:
这个问题与When using React Is it preferable to use fat arrow functions or bind functions in constructor? 类似,但有点不同。您可以在构造函数中将函数绑定到this,或者只在构造函数中应用箭头函数。请注意,我的项目中只能使用 ES6 语法。
1.
class Test extends React.Component{
constructor(props) {
super(props);
this.doSomeThing = this.doSomeThing.bind(this);
}
doSomething() {}
}
2.
class Test extends React.Component{
constructor(props) {
super(props);
this.doSomeThing = () => {};
}
}
这两种方式的优缺点是什么?谢谢。
【问题讨论】:
标签: javascript reactjs constructor ecmascript-6 arrow-functions