首页 > 其他分享 >js class super的作用

js class super的作用

时间:2024-07-14 15:19:29浏览次数:10  
标签:调用 name Parent js 父类 super class 构造函数

js super作用

在 JavaScript 中,super 关键字有两个主要用途:

  1. 在子类的构造函数中调用父类的构造函数

    • 使用 super() 来调用父类的构造函数,并继承父类的属性。
  2. 在子类的方法中调用父类的方法

    • 使用 super.methodName() 来调用父类的方法。

示例解释

使用类

class Parent {
  constructor(name) {
    this.name = name;
  }

  greet() {
    console.log(`Hello, my name is ${this.name}`);
  }
}

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

  greet() {
    super.greet();  // 调用父类的方法
    console.log(`I am ${this.age} years old`);
  }
}

const childInstance = new Child('Alice', 30);
childInstance.greet();
// 输出:
// Hello, my name is Alice
// I am 30 years old

详细解释

  1. 在构造函数中调用父类的构造函数

    在子类的构造函数中,必须调用 super(),以便调用父类的构造函数。这是因为子类没有自己的 this 对象,而是从父类继承的 this 对象。

    class Parent {
      constructor(name) {
        this.name = name;
      }
    }
    
    class Child extends Parent {
      constructor(name, age) {
        super(name);  // 调用父类的构造函数
        this.age = age;
      }
    }
    
  2. 在子类的方法中调用父类的方法

    在子类的方法中,可以使用 super.methodName() 来调用父类的方法。这允许子类在扩展或重写父类方法时仍然能够调用父类的方法。

    class Parent {
      greet() {
        console.log(`Hello, my name is ${this.name}`);
      }
    }
    
    class Child extends Parent {
      constructor(name, age) {
        super(name);
        this.age = age;
      }
    
      greet() {
        super.greet();  // 调用父类的方法
        console.log(`I am ${this.age} years old`);
      }
    }
    

使用静态方法

super 也可以用在静态方法中,调用父类的静态方法。

class Parent {
  static greet() {
    console.log('Hello from Parent');
  }
}

class Child extends Parent {
  static greet() {
    super.greet();  // 调用父类的静态方法
    console.log('Hello from Child');
  }
}

Child.greet();
// 输出:
// Hello from Parent
// Hello from Child

说明

  • super() 只能在构造函数中使用,必须在使用 this 之前调用。
  • super.methodName() 可以在子类的任何方法中使用,用于调用父类的方法。
  • 在静态方法中,super.methodName() 调用父类的静态方法。

通过这种方式,super 关键字在 JavaScript 中提供了一种在子类中访问和调用父类构造函数和方法的机制,使得继承和代码复用更加简单和直观。

js class super编译成es5语法等同于

为了将 classsuper 关键字转换为等价的函数和原型语法,我们需要详细地理解它们的行为,并确保在转换过程中保留其功能。

以下是一个完整的例子,展示了如何将使用 classsuper 的代码转换为等效的使用函数构造函数和原型链的代码。

使用 classsuper 的代码

class Parent {
  constructor(name) {
    this.name = name;
  }

  greet() {
    console.log(`Hello, my name is ${this.name}`);
  }

  static staticMethod() {
    console.log('This is a static method in Parent class.');
  }
}

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

  greet() {
    super.greet();  // 调用父类的方法
    console.log(`I am ${this.age} years old`);
  }

  static staticMethod() {
    super.staticMethod();  // 调用父类的静态方法
    console.log('This is a static method in Child class.');
  }
}

const childInstance = new Child('Alice', 30);
childInstance.greet();
Child.staticMethod();

编译为使用函数和原型链的代码

function Parent(name) {
  this.name = name;
}

Parent.prototype.greet = function() {
  console.log(`Hello, my name is ${this.name}`);
};

Parent.staticMethod = function() {
  console.log('This is a static method in Parent class.');
};

function Child(name, age) {
  Parent.call(this, name);  // 调用父类的构造函数
  this.age = age;
}

// 继承 Parent 的原型方法
Child.prototype = Object.create(Parent.prototype);
Child.prototype.constructor = Child;

Child.prototype.greet = function() {
  Parent.prototype.greet.call(this);  // 调用父类的方法
  console.log(`I am ${this.age} years old`);
};

// 继承 Parent 的静态方法
Child.staticMethod = function() {
  Parent.staticMethod.call(this);  // 调用父类的静态方法
  console.log('This is a static method in Child class.');
};

const childInstance = new Child('Alice', 30);
childInstance.greet();
Child.staticMethod();

详细解释

  1. 构造函数和实例属性

    • 在类中,使用 constructor 方法来定义构造函数,并使用 super(name) 调用父类构造函数。
    • 在函数构造函数中,使用 Parent.call(this, name) 调用父类构造函数。
  2. 原型方法

    • 在类中,定义方法不使用 static 关键字的方法,这些方法添加到类的原型对象上。
    • 在函数构造函数中,使用 Parent.prototype.methodName 来定义原型方法。
  3. 静态方法

    • 在类中,使用 static 关键字定义静态方法。
    • 在函数构造函数中,直接在构造函数对象上添加静态方法。
  4. 调用父类的方法和静态方法

    • 在类中,使用 super.methodName() 调用父类的方法。
    • 在函数构造函数中,使用 Parent.prototype.methodName.call(this) 调用父类的方法,以及使用 Parent.staticMethod.call(this) 调用父类的静态方法。

通过这种方式,所有使用 classsuper 的功能在使用函数构造函数和原型链的代码中得到了保留和实现。

标签:调用,name,Parent,js,父类,super,class,构造函数
From: https://www.cnblogs.com/jocongmin/p/18301603

相关文章

  • vue.js下载安装
    参考——https://www.jb51.net/article/283884.htm 注:使用的是vue2进入官网https://cn.vuejs.org/文档——》vue2文档 或者直接通过这里https://v2.cn.vuejs.org/v2/guide/复制绿色部分,粘贴到浏览器https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js ......
  • es5 js函数有哪几种继承方式
    在ES5(ECMAScript5)中,JavaScript函数有几种继承方式,主要是通过原型链实现的。以下是常见的几种继承方式:原型链继承(PrototypeInheritance):原理:通过将子类的原型对象设置为父类的实例来实现继承。特点:可以继承父类的实例方法和属性,但无法实现多继承。示例:functionParent(na......
  • Sortable.js板块拖拽示例
    图例代码在图片后面点赞❤️+关注......
  • Highlight.js示例
    图例 代码在图片后面点赞❤️+关注......
  • js实现归并排序算法
    在JavaScript中实现归并排序可以通过递归的方式完成。归并排序使用了“分而治之”的策略,将数组递归地分成两个子数组,分别进行排序,然后将它们合并成一个有序数组。以下是一个简单的归并排序实现:functionmergeSort(arr){//如果数组只有一个元素或为空,则直接返回数组......
  • js实现快速排序算法
    在JavaScript中实现快速排序可以通过递归方式来完成。下面是一个示例代码:functionquickSort(arr){//如果数组为空或只有一个元素,则无需排序if(arr.length<=1){returnarr;}//选择基准元素(这里选择中间元素)constpivotIndex=Math.fl......
  • 【反悔贪心】P2949WorkSchedulingG+P4053[JSOI2007]建筑抢修题解
    这两天遇到了几个很神奇的题目——能反悔的贪心。赶紧记录一下。例1(用时一定模型)用时一定:每个任务完成的耗时都是一样的。题面:Luogu-P2949WorkSchedulingG大体思路是:先把所有任务按照截止时间从小到大排序,然后枚举,遇到一个能做任务的就把他做了,把他的贡献加入一个......
  • three.js+vue污水处理厂数字孪生平台智慧城市web3d
    案例效果截图如下: 主场景三维逻辑代码如下:<template><divclass="whole"><!--threejs画布--><divid="threejs"ref="threejs"></div><!--污水厂模型加载进度条--><a-progress:stroke-colo......
  • 048基于SSM+Jsp的教学质量评价系统
     开发语言:Java框架:ssm技术:JSPJDK版本:JDK1.8服务器:tomcat7数据库:mysql5.7(一定要5.7版本)数据库工具:Navicat11开发软件:eclipse/myeclipse/ideaMaven包:Maven3.3.9系统展示管理员登录管理员功能学院管理学生管理教师管理督导管理教师信息管理学生评教管理督导评......
  • IO输入输出流例子:Java对象输出json文本:
    读取文件:原始字节输入流(低级):publicclassCharCacheIOReader{publicstaticvoidmain(String[]args){try(//原始字节输入流(低级)Readerfr=newFileReader("src\\OutputStream.txt");//创建一个字......