我刚开始在 React 中使用 ES6 类,之前我一直将我的方法绑定到当前对象(在第一个示例中显示),但是 ES6 是否允许我使用箭头将类函数永久绑定到类实例?(当作为回调函数传递时很有用。)当我尝试像 CoffeeScript 一样使用它们时出现错误:
class SomeClass extends React.Component {
  // Instead of this
  constructor(){
    this.handleInputChange = this.handleInputChange.bind(this)
  }
  // Can I somehow do this? Am i just getting the syntax wrong?
  handleInputChange (val) => {
    console.log('selectionMade: ', val);
  }
因此,如果我要传递SomeClass.handleInputChange给,例如setTimeout,它的范围将限定为类实例,而不是window对象。