关于 JavaScript 绑定方法

About JavaScript bind method

reference code segment

this.localizationChanged = this.localizationChanged.bind(this);

谁能告诉我为什么要这样写?

The bind() method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.

来自 MDN.

Who can tell me why to write like that?

localizationChanged 用作事件处理程序:

LocalizationStore.addChangeListener(this.localizationChanged);`

如果处理程序未绑定到组件实例,this 将不会引用组件实例并且将无法调用组件的 setState 方法(this.setState(...)).

如果您尝试在 localizationChanged 函数中访问它,请绑定它。

然而在 ES2015 中你不需要 this.You 可以使用箭头操作符 :

localizationChanged =()=>{
    console.log(this);
}