https://reactjs.org/docs/handling-events.html
class Toggle extends React.Component {
constructor(props) {
super(props);
this.state = {isToggleOn: true};
// This binding is necessary to make `this` work in the callback
this.handleClick = this.handleClick.bind(this);
}
handleClick() {
// ...
}
// ...
}
以下可以直接复制到Chrome控制台运行
var obj = {
key: 'obj',
handle: function(){
console.log(this)
}
}
obj.handle() // {key: "obj", handle: ƒ}
handle.bind(obj)() // obj.handle()
// assign directly
var assignMethod = obj.handle
assignMethod() // Window {postMessage: ƒ, blur: ƒ, focus: ƒ, close: ƒ, frames: Window, …}
// assign used bind
assignMethod = obj.handle.bind(obj)
assignMethod() // {key: "obj", handle: ƒ}