• 一文-学会es6的class类


    看完文章会收获满满哦!

     一、类的定义和声明

    1. class Person {} // 类的声明
    2. const Girl = class {} // 类的表达式

     二、类中的方法

    1.构造器方法

     一个类只能有一个构造函数
     通过new关键字操作类时会调用constructor函数,如果不写也会调用默认的constructor

    1. class Person {
    2. constructor(name, age) {
    3. this.name = name;
    4. this.age = age;
    5. }
    6. }
    7. const p1 = new Person('wyy',22)
    8. console.log(p1)

    2.实例方法--通过创建的实例进行访问

    1. class Person {
    2. constructor(name, age) {
    3. this.name = name;
    4. this.age = age;
    5. }
    6. // 实例方法
    7. eating() {
    8. console.log(this.name, "eating");
    9. }
    10. }
    11. const p1 = new Person('wyy',22)
    12. p1.eating() // wyy eating

    3.访问器方法

     可对类的属性操作进行访问和响应拦截

    1. class Person {
    2. constructor(name, age) {
    3. this.name = name;
    4. this.age = age;
    5. this._sex = "boy";
    6. }
    7. // 类的访问器方法
    8. get sex() {
    9. console.log("拦截访问操作");
    10. return this._sex;
    11. }
    12. set sex(newVal) {
    13. console.log("拦截设置操作");
    14. this._sex = newVal;
    15. }
    16. }
    17. const p1 = new Person('wyy',22)
    18. p1.sex = '男'
    19. console.log(p1.sex)

     4.静态方法-通过类名访问

    如Promise.all()

    1. class Person {
    2. constructor(name, age) {
    3. this.name = name;
    4. this.age = age;
    5. this._sex = "boy";
    6. }
    7. // 静态方法
    8. static creatP() {
    9. console.log("访问了类的静态方法");
    10. }
    11. }
    12. Person.creatP();

    三、类的继承

    类的继承使用extends关键字

    1. class Boy {}
    2. class Student extends Boy {}

    super:调用父类的构造器函数

    注:在子类的构造函数中在使用this之前必须调用父类的构造方法(super)

            使用位置:子类(派生类)的构造函数,实例方法,静态方法

    1.继承父类的属性 

    1. // 类的继承
    2. class Boy {
    3. constructor(name, age) {
    4. this.name = name;
    5. this.age = age;
    6. }
    7. }
    8. class Student extends Boy {
    9. constructor(name, age, sno) {
    10. super(name, age); //调用父类的构造函数
    11. this.sno = sno;
    12. }
    13. }
    14. const s1 = new Student("hhh", 18, "2339929");
    15. console.log(s1);

    2.继承父类的方法

    1. class Boy {
    2. constructor(name, age) {
    3. this.name = name;
    4. this.age = age;
    5. }
    6. say() {
    7. console.log(this.name, "say");
    8. }
    9. }
    10. class Student extends Boy {
    11. constructor(name, age, sno) {
    12. super(name, age); //调用父类的构造函数
    13. this.sno = sno;
    14. }
    15. //定义子类内部自己的方法
    16. study() {
    17. console.log(this.name, "study");
    18. }
    19. }
    20. const s1 = new Student("hhh", 18, "2339929");
    21. s1.say(); //方法的继承

    3.对父类方法进行重写

    如果对父类的方法不满意时,可根据需求进行重写

    3.1 完全重写

    1. class Boy {
    2. constructor(name, age) {
    3. this.name = name;
    4. this.age = age;
    5. }
    6. say() {
    7. console.log(this.name, "say");
    8. }
    9. }
    10. class Student extends Boy {
    11. constructor(name, age, sno) {
    12. super(name, age); //调用父类的构造函数
    13. this.sno = sno;
    14. }
    15. say() {
    16. console.log(this.name, "子类重写say");
    17. }
    18. }
    19. const s1 = new Student("hhh", 18, "2339929");
    20. s1.say();

    3.2 部分重写-通过super关键字实现

    1. class Boy {
    2. constructor(name, age) {
    3. this.name = name;
    4. this.age = age;
    5. }
    6. say() {
    7. console.log(this.name, "say1");
    8. console.log(this.name, "say2");
    9. console.log(this.name, "say3");
    10. }
    11. }
    12. class Student extends Boy {
    13. constructor(name, age, sno) {
    14. super(name, age); //调用父类的构造函数
    15. this.sno = sno;
    16. }
    17. say() {
    18. super.say()
    19. console.log(this.name, "子类重写say1");
    20. console.log(this.name, "say2");
    21. }
    22. }
    23. const s1 = new Student("hhh", 18, "2339929");
    24. s1.say();

    3.3 对静态方法进行重写

    1. class Boy {
    2. static sum() {
    3. console.log("static静态方法");
    4. }
    5. }
    6. class Student extends Boy {
    7. // 重写静态方法
    8. static sum() {
    9. super.sum();
    10. console.log("子类的静态方法");
    11. }
    12. }
    13. Student.sum();

  • 相关阅读:
    DevOps | 产研协同效能提升之评审、审批流、质量卡点
    MySQL - 触发器
    Linux Cgroup 系列:CentOS 7 Systemd Cgroup 层级
    详解 canal 同步 MySQL 增量数据到 ES
    判断船进去倾倒区次数
    使用字节流读取文件中的数据的几种方式
    【前端】学习前端vue框架MVVM模式
    Webapp中完成资源的跳转:转发和重定向
    【Final Project】Kitti的双目视觉里程计(1)
    基于python+django的美食餐厅点餐订餐网站
  • 原文地址:https://blog.csdn.net/weixin_57399180/article/details/125502804