React: 构造函数(constructor)中的super()和super(props)
leslie1943 opened this issue · comments
1. 定义class组件,为什么要加上super()
class App extends Component{
constructor(){
// super()
this.state = {name: 'su'} // ❌ 'this' is not allowed before super() => Reference Error
this.handleChange = this.handleChange.bind(this)
}
}
2. super的作用究竟是什么?
super
关键字, 它指代父类的实例(也就是父类的this
对象)- 子类必须在
constructor
函数中调用super
方法, 否则新建实例时会报错. - 这是因为子类没有自己的
this
对象,而是继承自父类的this
对象 - 总而言之: 子类如果不在
constructor()
中调用super()
,子类就得不到this
对象
3. super() vs super(props)
- 只有一个理由需要传递
props
作为super
的参数,就是希望在构造函数中使用this.props