浅谈JavaScript的几种继承实现方式

yizhihongxing

浅谈JavaScript的几种继承实现方式

JavaScript是一种支持面向对象编程的语言,也支持多种继承实现方式。本文将介绍JavaScript中几种常见的继承实现方式,以及它们的优缺点。

1. 原型链继承

原型链继承是JavaScript最基本、最常见的继承方式。通过让子类原型指向父类实例,从而实现子类继承父类的属性和方法。

实现方式

function Parent() {
  this.name = 'parent';
  this.names = ['parent1', 'parent2'];
}

Parent.prototype.sayName = function() {
  console.log(this.name);
}

function Child() {
  this.name = 'child';
}

Child.prototype = new Parent(); // 原型链继承

const child1 = new Child();
console.log(child1.name); // "child"
child1.sayName(); // "child"
console.log(child1.names); // ["parent1", "parent2"]

优缺点

  • 优点:简单易懂,容易实现;
  • 缺点:
  • 父类构造函数中的引用类型属性会被所有子类实例共享;
  • 无法实现多继承;

2. 借用构造函数

又称为经典继承,通过调用父类的构造函数来实现子类继承父类的属性和方法,从而避免了原型链继承中子类实例共享父类引用类型属性的问题。

实现方式

function Parent() {
  this.names = ['parent1', 'parent2'];
}

function Child() {
  Parent.call(this); // 借用 Parent 的构造函数
  this.name = 'child';
}

const child1 = new Child();
console.log(child1.name); // "child"
console.log(child1.names); // ["parent1", "parent2"]

优缺点

  • 优点:
  • 避免了原型链继承中父类引用类型属性被所有子类实例共享的问题;
  • 可以在子类构造函数中向父类传递参数;
  • 缺点:
  • 方法都在构造函数中定义,新建实例时都会创建一次,无法实现复用;
  • 无法实现函数复用,父类原型上的方法无法被子类调用;

3. 组合继承

组合继承即将原型链继承与借用构造函数组合起来,通过子类构造函数中调用父类构造函数,实现子类实例继承父类构造函数中定义的属性和方法,通过子类原型指向父类实例实现子类实例继承父类原型上定义的属性和方法。

实现方式

function Parent(name) {
  this.name = name;
  this.colors = ['red', 'green', 'blue'];
}

Parent.prototype.sayName = function() {
  console.log("My name is " + this.name);
}

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

Child.prototype = new Parent();
Child.prototype.constructor = Child;

Child.prototype.sayAge = function() {
  console.log("I'm " + this.age + " years old.");
}

const child1 = new Child('Tom', 18);
child1.colors.push('yellow');
console.log(child1.colors); // ["red", "green", "blue", "yellow"]
child1.sayName(); // "My name is Tom"
child1.sayAge(); // "I'm 18 years old."

优缺点

  • 优点:
  • 既可以实现原型链继承,又可以避免父类引用类型属性被子类实例共享;
  • 可以在子类构造函数中向父类传递参数;
  • 父类原型上的方法可以复用;
  • 缺点:父类构造函数在子类原型上执行了一次,导致子类实例的构造函数会多执行一次。

4. ES6 Class

ES6中提供了class关键字,可以更加简洁、直观地实现继承,并且支持继承多个类。

实现方式

class Parent {
  constructor(name) {
    this.name = name;
    this.colors = ['red', 'green', 'blue'];
  }

  sayName() {
    console.log("My name is " + this.name);
  }
}

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

  sayAge() {
    console.log("I'm " + this.age + " years old.");
  }
}

const child1 = new Child('Tom', 18);
child1.colors.push('yellow');
console.log(child1.colors); // ["red", "green", "blue", "yellow"]
child1.sayName(); // "My name is Tom"
child1.sayAge(); // "I'm 18 years old."

优缺点

  • 优点:
  • 语法简洁,易读易懂;
  • 支持多继承(extends后可以跟多个父类);
  • 缺点:无法实现动态继承。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:浅谈JavaScript的几种继承实现方式 - Python技术站

(0)
上一篇 2023年6月26日
下一篇 2023年6月26日

相关文章

  • stringformat左补0字符串

    String.Format左补0字符串 在C#中,我们可以使用String.Format方法来格式化字符串。其中,左补0字符串是一种常见的格式化方式,可以将数字字符串左侧补0,使其达到指定的位数。以下是String.Format左补0的完整攻略。 步骤 以下是使用String.Format左补0字符串的步骤: 使用String.Format方法格式化字符串。…

    other 2023年5月6日
    00
  • 修改jar包package目录结构操作方法

    修改jar包package目录结构操作方法一般需要进行以下步骤: 将jar包解压,可以使用例如winrar等压缩软件进行解压,将jar包中的所有文件解压到一个单独的文件夹中。 找到需要修改的包目录,将目录和其中的类文件复制到新的包路径中。例如,若需要将com.example.original包中的所有类移动到com.example.new包中,需要在解压出的…

    other 2023年6月26日
    00
  • 深入理解springboot中配置文件application.properties

    下面我将详细讲解“深入理解springboot中配置文件application.properties”的完整攻略: 什么是application.properties application.properties 是 Spring Boot 应用程序中的默认配置文件。它支持基于属性键值对的配置方式。在 application.properties 文件中,可…

    other 2023年6月25日
    00
  • adbdevicesunauthorized的解决办法

    “adb devices unauthorized”是指在使用Android Debug Bridge(ADB)连接设备时,设备未被授权,无法进行调试。下面是”adb devices unauthorized”的解决办法的完整攻略,包括两个示例说明。 方法一:重置ADB授权 在设备未被授权时,我们可以尝试重置ADB授权,以重新授权设备。下面是一个示例,用于演…

    other 2023年5月9日
    00
  • 在次封装easyui-Dialog插件实现代码

    在此封装easyui-Dialog插件实现代码,主要包括以下两个步骤: 引入easyui-Dialog插件库和封装代码文件 编写调用代码,实现弹窗功能 以下是详细步骤说明: 1. 引入easyui-Dialog插件库和封装代码文件 首先需要在网站中引入easyui-Dialog插件库,可以通过以下方式实现: <!– 引入easyui插件库 –&gt…

    other 2023年6月25日
    00
  • java Person,Student,GoodStudent 三个类的继承、构造函数的执行

    三个类的继承关系如下: Person | Student | GoodStudent 其中,Person是父类,Student是子类,GoodStudent是Student的子类。即Student继承了Person类,GoodStudent继承了Student类。 在Java中,子类的构造函数中会默认调用父类的空参构造函数。若父类没有空参构造函数,则需要在子…

    other 2023年6月26日
    00
  • 分析设计模式之模板方法Java实现

    分析设计模式之模板方法是一种行为型设计模式,它定义了一个操作中的算法骨架,将一些步骤延迟到子类中实现,使得子类可以不改变一个算法的骨架结构,即可重定义该算法的某些特定步骤。以下是完整的攻略: 模板方法Java实现 1. 定义抽象类 首先,我们需要定义一个抽象类,即模板类,该类中包含算法骨架和一些基本方法。这些基本方法可以是抽象方法、具体方法和钩子方法。 pu…

    other 2023年6月26日
    00
  • 腾讯QQ6.5 v12945 正式版发布(附官方下载地址)

    腾讯QQ6.5 v12945 正式版发布攻略 介绍 腾讯QQ6.5 v12945 正式版是腾讯公司最新发布的QQ即时通讯软件版本。本攻略将详细介绍如何下载、安装和使用该版本的QQ,并提供两个示例说明。 下载 你可以通过以下步骤下载腾讯QQ6.5 v12945 正式版: 访问腾讯QQ官方网站:https://im.qq.com/ 在官方网站首页,找到并点击“下…

    other 2023年8月3日
    00
合作推广
合作推广
分享本页
返回顶部