创建 React 类时,哪个更可取?
export default class Foo extends React.Component {
constructor (props) {
super(props)
this.doSomething = this.doSomething.bind(this)
}
doSomething () { ... }
}
或者
export default class Foo extends React.Component {
doSomething = () => { ... }
}
我的一个同事认为后者会导致内存问题,因为 babel 将代码转译为this在闭包内部捕获,并且该引用将导致实例无法被 GC 清理。
对此有何想法?