zgwyfz 2019-11-04
写在前面
ES6后,Class的出现极大地简化了JavaScript的继承。
不过原型是JavaScript的基础,也是JavaScript的精髓所在。对于原型继承,请参考另一篇文章:《JavaScript · 原型继承》
Class继承
class Student { constructor(name) { this.name = name; } getName() { console.log(this.name); } } class PrimaryStudent extends Student { constructor(name, grade) { super(name); this.grade = grade; } getName() { super.getName(); console.log(`Nickname: 'mi'`); } getGrade() { console.log(this.grade); } } var o1 = new Student('lala'); var o2 = new PrimaryStudent('fafa','grade 4'); o1 // Student {name: "lala"} o1.getName(); // lala o2 // PrimaryStudent {name: "fafa", grade: "grade 4"} o2.getName(); // fafa Nickname:mi o2.getGrade(); // grade 4