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

js的继承的方式

1.对象冒充继承
使用 bind,call,apply 解决构造函数属性的继承
缺点:不能继承原型上的属性和方法

  //-------------父类-------------function Person(name, age, sex) {this.name = name;this.age = age;this.sex = sex;}Person.prototype.run = function () {console.log(`${this.name},爱跑步!`);}//-------------子类-------------function Student(sNo, name, age, sex) {//对象的冒充// bind,call,applyPerson.call(this, name, age, sex);//后面this.sNo = sNo;}var s1 = new Student(10001, "刘德华", 20, "男");console.log(s1);s1.run()

2.原型链继承
缺点:不能让构造函数的属性,初始化

   //-------------父类-------------function Person(name, age) {this.name = name;this.age = age;}Person.prototype.name = "刘德海";Person.prototype.run = function () {alert(123);}//-------------子类-------------function Student() {}//我只实现了原型继承,构造函数内部的属性,是无法设置初始值Student.prototype = new Person();var s2 = new Student();console.log(s2);// s2.run()console.log(s2.address);

3.组合继承(对象冒充+原型链继承)
缺点:原型中会有多余的属性,并且是undefined

   //-------------父类-------------function Person(name, age) {this.name = name;this.age = age;}Person.prototype.name = "刘德海";Person.prototype.run = function () {alert(123);}//-------------子类-------------function Student() {}//我只实现了原型继承,构造函数内部的属性,是无法设置初始值Student.prototype = new Person();var s2 = new Student();console.log(s2);// s2.run()console.log(s2.address);

4.寄生组合继承
寄生继承+对象冒充继承=寄生组合继承
寄生组合继承 Object.create(base.prototype);

   // 寄生继承,解决原型问题// 寄生继承+对象冒充继承=寄生组合继承function inherit_proto(base, child) {// 1.创建父类原型,根据父类的原型,创建一个新的对象var basePrototype = Object.create(base.prototype);// 2.创建的原型对象,它的构造还是指向原来的构造函数// 我们就修改子类的构造器basePrototype.constructor = child// 3.让自己拥有父类的原型child.prototype = basePrototypeconsole.log(basePrototype);}// 父类function Person(name, age, sex) {this.name = namethis.age = agethis.sex = sex}Person.prototype.sayHi = function () { }// 子类function Student(sNo, name, age, sex) {Person.call(this, name, age, sex)this.sNo = sNo}// 调用方法inherit_proto(Person, Student)var stu = new Student("1001", "小易", 22, "女")console.log(stu);

5.ES6的类+extends继承

  class Person {constructor(name, age) {this.name = namethis.age = age}run(){return  `跑步`}}class Student extends Person {constructor(name, age, sex) {super(name, age)this.sex = sex}// 重写:子类重写父类的方法run(){return  `哈哈哈哈哈哈或`}}var p=new Student("小易",22,"女")console.log(p.run());console.log(p);
http://www.lryc.cn/news/190577.html

相关文章:

  • 聊聊HttpClient的重试机制
  • 北邮22级信通院数电:Verilog-FPGA(4)第三周实验:按键消抖、呼吸灯、流水灯 操作流程注意事项
  • Ghidra101再入门(上?)-Ghidra架构介绍
  • Vue3路由引入报错解决:无法找到模块“xxx.vue”的声明文件 xxx隐式拥有 “any“ 类型。
  • 基于若依ruoyi-nbcio支持flowable流程分类里增加流程应用类型
  • JS之同步异步promise、async、await
  • 【OpenCV • c++】自定义直方图 | 灰度直方图均衡 | 彩色直方图均衡
  • el-tree目录和el-table实现搜索定位高亮方法
  • linux常用指令
  • C语言,指针的一些运算
  • iPhone 如何强制重启
  • 数据结构--单链表操作
  • AlmaLinux (兼容centos)安装Geant4与ROOT
  • FPGA面试题(2)
  • 【C++ Primer Plus学习记录】指针——使用new来创建动态数组
  • 移动app广告变现,对接广告联盟还是选择第三方聚合广告平台?
  • ARM 按键控制 LED灯,蜂鸣器,风扇
  • VirtualBox Ubuntu扩展虚拟机磁盘空间
  • C#开发的OpenRA游戏之电力系统之二
  • Java架构师基础框架设计
  • tortoise创建本地仓库
  • 【FreeRTOS】【STM32】03 FreeRTOSConfig.h头文件简介与修改
  • VScode商店无法访问
  • 【UnityUGUI】复合控件详解,你还记得多少
  • ubuntu下使用gcc编译c程序: “error: stray ‘\357’ in program“
  • LeetCode 143.重排链表
  • 不做决策的“RPA机器人”,不是合格的“数字化劳动力”
  • 网页开发中使用highlight.js实现代码高亮 + 行号
  • 访问Apache Tomcat的manager页面
  • Ubuntu 20.04.6 LTS repo int 提示/usr/bin/env: “python“: 权限不够