super关键字

tips:子类会自动继承父类的方法
在继承的构造函数中super必须放在第一行

  1. class Father{
  2. constructor(x,y){
  3. this.x=x;
  4. this.y=y;
  5. }
  6. count(x,y){
  7. console.log(x+y);
  8. }
  9. }
  10. class Son extends Father{
  11. constructor(x,y){
  12. //在继承的构造函数中super必须放在第一行
  13. super(x,y);
  14. }
  15. }
  16. var son=new Son();
  17. son.count(2,4)

demo

  1. class Person{
  2. constructor(name,age){
  3. this.name=name;
  4. this.age=age
  5. }
  6. sayName(){
  7. console.log(this.name);
  8. }
  9. }
  10. class Student extends Person{
  11. constructor(name,age,skill){
  12. super(name,age);
  13. this.skill=skill;
  14. }
  15. }
  16. var s=new Student("李思",19,"wechat")
  17. s.sayName()
  18. console.log(s);