总结:1.类中的构造器不是必须的,要对实例进行初始化的操作,如添加指定属性时
2.如果A类继承了B类,且在A类中写了构造器,那么A类中构造器中的super是必须要调用的
3.类所有的方法都放在了类的原型对象上,供实例去使用
1.创建一个类
class Person {
constructor(name, age) {
this.name = name;
this.age = age
}
speak() {
console.log(`我叫${this.name}`)
}
}
const p1 = new Person('tom', 18)
const p2 = new Person('marry', 18)
2.类的继承
class Student extends Person {
constructor(name, age, grade) {
super(name, age, grade)
this.name = name;
this.age = age;
this.grade = grade;
}
speak() {
console.log(`我叫${this.name}`)
}
study() {
console.log('123')
}
}
const s1 = new Student('amy', 16)