当前位置: 首页 > news >正文

js之继承

1.原型链继承(Prototypal Inheritance)

原型链继承是完成继承最直接的方式。这会涉及到两个过程:创建新对象和重置构造器引用。首先,我们创建一个新构造函数,然后将它的prototype属性指向一个父类的实例。

function Parent() {this.name = 'parent';
}function Child() {this.age = 27;
}Child.prototype = new Parent();
var child = new Child();console.log(child.name); // 输出: "parent"

2.构造函数继承(Constructor Inheritance)

这种方法共享了方法,在子构造函数中通过call或apply方法调用父构造函数。这样做可以使子构造函数具有父构造函数的所有属性,并且所有的属性方法都不会被共享。

function Parent() {this.names = ["John", "Stella"];
}function Child() {Parent.call(this);
}var child1 = new Child();
var p = new Parent();child1.names.push("New name");console.log(child1.names); // 输出: ["John", "Stella", "New name"]
console.log(p.names); // 输出: ['John', 'Stella']

3.组合继承(Combination Inheritance)

组合继承结合了原型链继承和构造函数继承的方式,通过在prototype上定义方法实现函数复用,又能保证每个实例有自己的属性。

function Parent(name) {this.names = ["John", "Stella"];this.name = name;
}Parent.prototype.getName = function() {console.log(this.name);
}function Child(name) {Parent.call(this, name);
}Child.prototype = new Parent();
Child.prototype.constructor = Child;var child1 = new Child("Tom");console.log(child1.names); // 输出: ["John", "Stella"]
child1.getName(); // 输出: "Tom"

4.原型式继承(Prototypal Inheritance)

这是ES5引入的Object.create方法实现的原型式继承,我们选中一个对象作为创建新的对象的基础。

var parent = {names: ["John", "Stella"]
};var child = Object.create(parent);
child.names.push("New name");console.log(child.names); // 输出: ["John", "Stella", "New name"]

5.寄生式继承(Parasitic Inheritance)

寄生式继承的思路是创造一个用作继承的父对象的副本,并增强这个副本,然后返回这个副本。

function createAnother(original) {var clone = Object.create(original);clone.sayHi = function() {console.log("Hi!");};return clone;
}var parent = {names: ["John", "Stella"]
};var child = createAnother(parent);child.sayHi(); // 输出: "Hi!"

6.寄生组合式继承(Parasitic Combination Inheritance)

寄生组合式继承(也被称为类式继承)是指通过借用构造函数来继承属性,通过原型链的形式来继承方法。本质上,该方法只调用了一次父类构造函数,并且避免在子类.prototype上创建不必要的、多余的属性。

function Parent(name) {this.name = name;this.colors = ['red', 'blue', 'green'];
}Parent.prototype.getName = function() {return this.name;
}function Child(name, age) {Parent.call(this, name); // 通过借用构造函数来继承属性this.age = age;
}Child.prototype = Object.create(Parent.prototype); // 通过原型链的形式来继承方法
Child.prototype.constructor = Child;var child1 = new Child("Tom", 22);console.log(child1.name); // 输出: "Tom"
child1.getName(); // 输出: "Tom"

7.ts里通过extends关键字实现继承

class DaChao extends Parent {}

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • WebGL之灯光使用解析
  • 查询IP地址保障电商平台安全
  • [VulnHub靶机渗透] Nullbyte
  • Day16:HTTP协议、Spring MVC、Thymeleaf模版引擎、Spring处理浏览器请求实例(传入和传出)、MyBatis
  • Spring Boot中Excel数据导入导出的高效实现
  • Linux--基础命令
  • 在linux上部署yolov5和安装miniconda3
  • Nestjs与Vue实现多人聊天[简易版]
  • Java中的常见类“Math”(一)用法详解
  • 项目中spring security与jwt.腾讯面试分享
  • ARM GNU 汇编 “每日读书“
  • 2.1基本算法之枚举1978:生理周期
  • opencv人脸识别实战2:刷脸功能(PyCharm实现)
  • 测试开发(6)软件测试教程——自动化测试selenium(自动化测试介绍、如何实施、Selenium介绍 、Selenium相关的API)
  • python INI文件操作与configparser内置库
  • 4. 路由到控制器 - Laravel从零开始教程
  • centos安装java运行环境jdk+tomcat
  • react 代码优化(一) ——事件处理
  • SpiderData 2019年2月13日 DApp数据排行榜
  • spring学习第二天
  • 编写高质量JavaScript代码之并发
  • 程序员该如何有效的找工作?
  • 前端技术周刊 2019-01-14:客户端存储
  • 如何进阶一名有竞争力的程序员?
  • 微信公众号开发小记——5.python微信红包
  • 微信小程序填坑清单
  • 系统认识JavaScript正则表达式
  • 用element的upload组件实现多图片上传和压缩
  • Semaphore
  • ​埃文科技受邀出席2024 “数据要素×”生态大会​
  • #AngularJS#$sce.trustAsResourceUrl
  • #调用传感器数据_Flink使用函数之监控传感器温度上升提醒
  • $.type 怎么精确判断对象类型的 --(源码学习2)
  • (2015)JS ES6 必知的十个 特性
  • (C++17) optional的使用
  • (c语言版)滑动窗口 给定一个字符串,只包含字母和数字,按要求找出字符串中的最长(连续)子串的长度
  • (ISPRS,2021)具有遥感知识图谱的鲁棒深度对齐网络用于零样本和广义零样本遥感图像场景分类
  • (pojstep1.1.1)poj 1298(直叙式模拟)
  • (阿里巴巴 dubbo,有数据库,可执行 )dubbo zookeeper spring demo
  • (附源码)springboot社区居家养老互助服务管理平台 毕业设计 062027
  • (附源码)计算机毕业设计高校学生选课系统
  • (六)vue-router+UI组件库
  • (全注解开发)学习Spring-MVC的第三天
  • (十七)devops持续集成开发——使用jenkins流水线pipeline方式发布一个微服务项目
  • (一)u-boot-nand.bin的下载
  • (原創) 如何解决make kernel时『clock skew detected』的warning? (OS) (Linux)
  • .htaccess配置常用技巧
  • .mp4格式的视频为何不能通过video标签在chrome浏览器中播放?
  • .mysql secret在哪_MYSQL基本操作(上)
  • .net Application的目录
  • .net core 客户端缓存、服务器端响应缓存、服务器内存缓存
  • .NET HttpWebRequest、WebClient、HttpClient
  • .NET 将混合了多个不同平台(Windows Mac Linux)的文件 目录的路径格式化成同一个平台下的路径
  • .NET 线程 Thread 进程 Process、线程池 pool、Invoke、begininvoke、异步回调
  • .net用HTML开发怎么调试,如何使用ASP.NET MVC在调试中查看控制器生成的html?