class App extends Component {
constructor(props) {
super(props);
this.addContact = this.addContact.bind(this);
console.log(this);
}
我不明白括号中最后一个“this”关键字的含义是什么。它代表什么?我知道当我想要使用方法时我应该绑定(bind)。但我在构造函数中绑定(bind)了什么?我没有向构造函数发送任何对象或其他内容,那么那里会发生什么?
我是一个自学成才的程序员新手,正在努力解决这个问题。你能帮我一下吗?
提前谢谢您。
最佳答案
What represent the
this
insidebind()
function
this
与您的代码相对应,在您的类App
如果您的方法 addContact
未绑定(bind)在 App
类的上下文中,则您无法使用此方法进行任何操作。
如果您的方法中不需要 this
上下文,也许您可以将此方法转换为静态方法。
请参阅此示例:MDN EXAMPLE
关于javascript - React Binding 中的 this 关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52030492/