javascript - React Binding 中的 this 关键字

标签 javascript reactjs binding

class App extends Component {
constructor(props) {
    super(props);
    this.addContact = this.addContact.bind(this);
    console.log(this);
}

我不明白括号中最后一个“this”关键字的含义是什么。它代表什么?我知道当我想要使用方法时我应该绑定(bind)。但我在构造函数中绑定(bind)了什么?我没有向构造函数发送任何对象或其他内容,那么那里会发生什么?

我是一个自学成才的程序员新手,正在努力解决这个问题。你能帮我一下吗?

提前谢谢您。

最佳答案

What represent the this inside bind() function

this 与您的代码相对应,在您的类App

如果您的方法 addContact 未绑定(bind)在 App 类的上下文中,则您无法使用此方法进行任何操作。

如果您的方法中不需要 this 上下文,也许您可​​以将此方法转换为静态方法。

请参阅此示例:MDN EXAMPLE

关于javascript - React Binding 中的 this 关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52030492/

相关文章:

javascript - 为什么当远程调试未激活时我的用户为空,而当其激活时它是一个对象?

javascript - 如何使用reactJS 模拟此 jQuery 行为 : add/remove className from different divs

maven - 如何抑制关于多个绑定(bind)的 SLF4J 警告?

c# - XAML 中的 WPF ListView 绑定(bind) ItemsSource

javascript - 在 Jquery Autocomplete 中遍历列表中的第一个值

javascript - 分隔数组的水线查询 "OR"

reactjs - Heroku : puppeteer chrome: error while loading shared libraries: libX11-xcb. so.1

javascript - 通过 Javascript 填充 ReactJS HTML 表单

windows - 在 Windows 中为套接字使用特定的网络接口(interface)

javascript - 通过 Javascript 设置复选框时避免 "ifChanged"事件处理程序