为什么类方法需要绑定?
Why do class methods need to be bound?
题目类型: 技术面试题
这是一道技术面试题,常见于澳洲IT公司面试中。
难度: hard
分类: JavaScript in React
标签: this binding, class methods, constructor, context
参考答案摘要
核心答案 在 JavaScript 中, this 的值取决于当前上下文。在 React 类组件的方法中,开发人员通常希望它引用组件的当前实例,因此有必要将这些方法绑定到该实例。通常这是在构造函数中完成的,例如: class SubmitButton extends React.Component { constructor(props) { super(props); this.state = ...
本题提供 STAR 原则详细解答和技术解析,登录匠人学院学习中心即可查看完整答案。