一、constructor or super
1.constructor
表示父类的构造方法,用来新建父类的this对象,这是ES6对类的默认方法,该方法是类中必须有的,若无显示定义,则会默认添加空的constructor( )方法。
2.super
在class方法中,继承是用 extends 关键字来实现。子类必须在 constructor( )调用 super( ),否则新建实例时会报错。
原因:子类没有自己的 this 对象,它只能继承自父类的 this 对象,对其进行加工,而super( )就是将父类中的this对象继承给子类;若没有 super,子类就得不到 this 对象。
无super时:
有super时:
结果正确
二、super or super(props)
若在constructor中要使用this.props,就必须给super加参数:super(props)
(无论有没有constructor,在render中this.props都可以使用,这是React自动附带的)