【ES6】js 中class的extends、super关键字用法和避坑点

在JavaScript中,使用class关键字可以实现面向对象编程。其中,extends和super是两个非常重要的关键字,它们分别用于实现类的继承和调用父类的方法。

一、extends关键字

extends关键字用于实现类的继承,它可以让一个子类继承父类的属性和方法。使用extends关键字时,需要指定要继承的父类,语法如下:

class 子类 extends 父类 {
  // 子类的属性和方法
}

例如,定义一个Person类和一个Student类,Student类继承自Person类:

class Person {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }
  sayHello() {
    console.log(`Hello, my name is ${this.name}`);
  }
}

class Student extends Person {
  constructor(name, age, grade) {
    super(name, age); // 调用父类的构造函数,这一行必须在this之前,否则报错。因为子类自己的this对象,必须先通过父类的构造函数完成塑造,得到与父类同样的实例属性和方法,然后再对其进行加工,添加子类自己的实例属性和方法。如果不调用super()方法,子类就得不到自己的this对象。
    this.grade = grade;
  }
  study() {
    console.log(`I'm studying...`);
  }
}

在上面的例子中,Student类继承了Person类的构造函数和方法,并且定义了自己的属性和方法。在构造函数中,使用super关键字来调用父类的构造函数,以便初始化父类的属性和方法。
注意:
上面的super(name, age)必须在this之前。因为子类自己的this对象,必须先通过父类的构造函数完成塑造,得到与父类同样的实例属性和方法,然后再对其进行加工,添加子类自己的实例属性和方法。如果不调用super()方法,子类就得不到自己的this对象。

二、super关键字

super关键字用于调用父类的方法。在子类的方法中,可以使用super关键字来调用父类的方法。使用super关键字时,需要指定要调用的父类方法,语法如下:

super(); // 调用父类的构造函数
super.父类方法(); // 调用父类的方法
super.属性; // 访问父类的属性

例如,在上面的例子中,在Student类的构造函数中使用了super关键字来调用父类的构造函数:

constructor(name, age, grade) {
  super(name, age); // 调用父类的构造函数
  this.grade = grade;
}

另外,在子类的方法中,也可以使用super关键字来调用父类的方法。例如:

class Person {
  constructor(name) {
    this.name = name;
  }
  sayHello() {
    console.log(`Hello, my name is ${this.name}`);
  }
}

class Student extends Person {
  constructor(name, grade) {
    super(name); // 调用父类的构造函数
    this.grade = grade;
  }
  sayHello() {
    super.sayHello(); // 调用父类的方法
    console.log(`I'm a student in grade ${this.grade}`);
  }
}

在上面的例子中,Student类继承了Person类,并重写了sayHello方法。在重写的sayHello方法中,使用super关键字来调用父类的sayHello方法。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
ES6super方法用于在子类调用父类的构造函数、静态方法或原型方法。它必须在使用this关键字之前调用,否则会抛出一个ReferenceError错误。 1. 调用父类构造函数 子类必须在构造函数调用super方法来执行父类的构造函数,以便正确地设置子类的this对象。例如: ``` class Animal { constructor(name) { this.name = name; } } class Dog extends Animal { constructor(name, breed) { super(name); this.breed = breed; } } const myDog = new Dog('Buddy', 'Golden Retriever'); console.log(myDog.name); // 'Buddy' console.log(myDog.breed); // 'Golden Retriever' ``` 在这个例子,Dog类继承自Animal类,因此它必须调用Animal类的构造函数来设置this.name属性。通过调用super(name),Dog类将传递给Animal构造函数的参数name。 2. 调用父类静态方法 子类可以调用父类的静态方法,通过使用super关键字。例如: ``` class Animal { static sayHi() { console.log('Hi from Animal'); } } class Dog extends Animal { static sayHi() { super.sayHi(); console.log('Hi from Dog'); } } Dog.sayHi(); // 'Hi from Animal' 'Hi from Dog' ``` 在这个例子,Dog类继承自Animal类,并覆盖了Animal的sayHi静态方法。通过使用super关键字,Dog类调用了Animal的sayHi静态方法,并在后面添加了自己的输出。 3. 调用父类原型方法 子类可以调用父类的原型方法,通过使用super关键字。例如: ``` class Animal { sayHi() { console.log('Hi from Animal'); } } class Dog extends Animal { sayHi() { super.sayHi(); console.log('Hi from Dog'); } } const myDog = new Dog(); myDog.sayHi(); // 'Hi from Animal' 'Hi from Dog' ``` 在这个例子,Dog类继承自Animal类,并覆盖了Animal的sayHi原型方法。通过使用super关键字,Dog类调用了Animal的sayHi方法,并在后面添加了自己的输出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

科学熊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值