首页 > 其他分享 >es5 js函数有哪几种继承方式

es5 js函数有哪几种继承方式

时间:2024-07-14 15:18:31浏览次数:7  
标签:function es5 name Parent 继承 js 哪几种 Child 构造函数

在 ES5(ECMAScript 5)中,JavaScript 函数有几种继承方式,主要是通过原型链实现的。以下是常见的几种继承方式:

  1. 原型链继承(Prototype Inheritance):

    • 原理:通过将子类的原型对象设置为父类的实例来实现继承。
    • 特点:可以继承父类的实例方法和属性,但无法实现多继承。
    • 示例:
      function Parent(name) {
        this.name = name;
      }
      
      Parent.prototype.greet = function() {
        console.log(`Hello, my name is ${this.name}`);
      };
      
      function Child(name, age) {
        this.age = age;
      }
      
      Child.prototype = new Parent();  // 原型链继承
      Child.prototype.constructor = Child;
      
      const childInstance = new Child('Alice', 30);
      childInstance.greet();  // 输出: Hello, my name is Alice
      
  2. 借用构造函数继承(Constructor Stealing / Classical Inheritance):

    • 原理:在子类的构造函数中调用父类的构造函数,使用 callapply 或者 bind 方法。
    • 特点:可以继承父类的实例属性,但不能继承父类的原型属性和方法。
    • 示例:
      function Parent(name) {
        this.name = name;
      }
      
      function Child(name, age) {
        Parent.call(this, name);  // 借用构造函数继承
        this.age = age;
      }
      
      const childInstance = new Child('Alice', 30);
      console.log(childInstance.name);  // 输出: Alice
      
  3. 组合继承(Combination Inheritance):

    • 原理:结合使用原型链继承和借用构造函数继承。
    • 特点:兼具原型链继承和借用构造函数继承的优点,是最常用的继承方式。
    • 示例:
      function Parent(name) {
        this.name = name;
      }
      
      Parent.prototype.greet = function() {
        console.log(`Hello, my name is ${this.name}`);
      };
      
      function Child(name, age) {
        Parent.call(this, name);  // 借用构造函数继承
        this.age = age;
      }
      
      Child.prototype = new Parent();  // 原型链继承
      Child.prototype.constructor = Child;
      
      const childInstance = new Child('Alice', 30);
      childInstance.greet();  // 输出: Hello, my name is Alice
      
  4. 原型式继承(Prototype Chain):

    • 原理:通过创建一个临时构造函数,将传入的对象作为这个构造函数的原型,实现对象的复制。
    • 特点:类似于浅拷贝,复制对象的属性,但是没有继承父类的构造函数。
    • 示例:
      const parent = {
        name: 'Parent',
        greet: function() {
          console.log(`Hello, my name is ${this.name}`);
        }
      };
      
      const child = Object.create(parent);
      child.name = 'Child';
      child.greet();  // 输出: Hello, my name is Child
      
  5. 寄生式继承(Parasitic Inheritance):

    • 原理:在原型式继承的基础上,增强对象,返回增强后的对象。
    • 特点:类似于工厂模式,创建一个函数来封装创建对象的过程。
    • 示例:
      function createChild(parent) {
        const child = Object.create(parent);
        child.age = 30;
        child.greet = function() {
          console.log(`Hello, my name is ${this.name}`);
        };
        return child;
      }
      
      const parent = {
        name: 'Parent'
      };
      
      const childInstance = createChild(parent);
      childInstance.greet();  // 输出: Hello, my name is Parent
      
  6. 寄生组合式继承(Parasitic Combination Inheritance):

    • 原理:结合借用构造函数继承和寄生式继承,通过构造函数来继承属性,并且通过原型链来继承方法。
    • 特点:避免了组合继承中重复调用父类构造函数的问题。
    • 示例:
      function Parent(name) {
        this.name = name;
      }
      
      Parent.prototype.greet = function() {
        console.log(`Hello, my name is ${this.name}`);
      };
      
      function Child(name, age) {
        Parent.call(this, name);  // 借用构造函数继承
        this.age = age;
      }
      
      Child.prototype = Object.create(Parent.prototype);  // 寄生式继承
      Child.prototype.constructor = Child;
      
      const childInstance = new Child('Alice', 30);
      childInstance.greet();  // 输出: Hello, my name is Alice
      

这些是在 ES5 中常见的 JavaScript 继承方式,每种方式都有其适用的场景和特点。选择合适的继承方式可以根据项目需求和设计模式来决定。

标签:function,es5,name,Parent,继承,js,哪几种,Child,构造函数
From: https://www.cnblogs.com/jocongmin/p/18301607

相关文章

  • 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");//创建一个字......
  • js 实现二分搜索算法
    二分算法搜索数字二分搜索算法是一种在有序数组中查找目标值的高效方法。其时间复杂度为(O(\logn))。下面是用JavaScript实现的二分搜索算法:functionbinarySearch(arr,target){letleft=0;letright=arr.length-1;while(left<=right){......
  • js 实现冒泡排序算法
    冒泡排序是一种简单的排序算法。它重复地遍历待排序的列表,比较相邻的元素并交换位置,如果它们的顺序错误。这个过程会重复进行,直到整个列表排序完成。下面是用JavaScript实现的冒泡排序算法:functionbubbleSort(arr){letn=arr.length;letswapped;do{......