React学习日记3
010 复习class
- 类中的构造器不是必须写的,要对实例化进行初始化操作,如添加指定属性时才行
- 如果A继承B,则A构造器中的super()必须调用
- 类中的方法都是放在类的原型对象上,供实例使用
class Person {
constructor(name) {
this.name = name
}
//方法是放在了类的原型对象上,供实例使用
//通过person实例调用方法,方法中的this指向实例对象
speak() {
console.log(this.name);
}
}
//实现继承
class Student extends Person {
constructor(name, grade) {
// 继承name,改变指针
super(name)
this.grade = grade
}
//重写从父类原型对象继承过来的方法
speak() {
console.log(this.name, this.grade);
}
}
const s1 = new Student('yy', 1)
011 类式组件