构造函数 组合式继承

这篇博客探讨了JavaScript中的组合式继承概念,通过实例展示了如何利用call、apply和bind方法来修改this指向,以及如何手动在原型上添加属性和方法。作者创建了Game和Map函数,并实现了Role函数作为子类,继承了Game和Map的特性。博客还提到了在原型链上挂载Role的方法,以及如何实例化和使用Role对象。整个示例展示了JavaScript面向对象编程的一种常见模式。
摘要由CSDN通过智能技术生成

目录

组合式继承

图例效果


    使用了call apply bind能够修改this指向的特点 进行了对象冒充

    但是这种方法 子类无法获取父类的原型上的属性和方法,所以组合式继承,手动在原型上添加名字和方法

组合式继承

将游戏名称和游戏地图继承在游戏角色上并挂载到原型

   function Game(name) { //被继承 1
        //游戏名称
        this.name = name
    }

    Game.prototype.play = function() { //在原型上添加play方法
        console.log('欢迎进入' + this.name)
    }

    function Map(mapName, num) { //被继承 2
        //地图名字 地图数量  
        this.mapName = mapName
        this.num = num
    }


    function Role(roleName, hit, sex) { //要继承的函数
        //角色信息
        this.roleName = roleName
        this.hit = hit
        this.sex = sex
        Map.call(this, '召唤师峡谷', 3) //给地图的信息 this指向函数中的对象
    }
    Role.prototype = new Game('英雄联盟') //实例化Game(游戏名称)赋值Role
    Role.prototype.constructor = Role

    let role = new Role('李白', '70', '男') //将Role实例化
    console.log(role); //继承了游戏名称,游戏地图,并且在原型上挂载了Role

图例效果

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAMC7mtLvlnKjpo47mtarph4w=,size_20,color_FFFFFF,t_70,g_se,x_16

更新中 ...

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
原型链继承(Prototype Inheritance)在JavaScript中是通过创建一个新对象并让它引用另一个对象的原型来实现的。例如: ```javascript function Parent() {} Parent.prototype.method = function() { console.log('Parent method'); }; let child = new Parent(); child.method(); // 输出: "Parent method" ``` **借用构造函数继承**(Constructo r Chaining)利用已有构造函数作为父类,通过`new`关键字传递给子类实例化过程,间接实现了继承: ```javascript function Parent() { this.parentProp = 'parent'; } function Child() { Parent.call(this); // 借用父类构造函数 this.childProp = 'child'; } Child.prototype = Object.create(Parent.prototype); Child.prototype.constructor = Child; let childInstance = new Child(); console.log(childInstance.parentProp); // 输出: "parent" console.log(childInstance.childProp); // 输出: "child" ``` **组合式继承**(Mix-in or Prototype Mixing)结合原型链和构造函数继承,允许从多个源继承属性和方法: ```javascript function Mixin(target) { for (let prop in Mixin.prototype) { target[prop] = Mixin.prototype[prop]; } } function Parent() { this.parentProp = 'parent'; } Mixin(Parent.prototype); let child = new Parent(); console.log(child.parentProp); // 输出: "parent" ``` **ES6的class类继承**(Class-based Inheritance)使用`extends`关键字实现: ```javascript class Parent { constructor() { this.parentProp = 'parent'; } parentMethod() { console.log('Parent method'); } } class Child extends Parent { constructor() { super(); this.childProp = 'child'; } childMethod() { console.log('Child method'); } } let childInstance = new Child(); childInstance.parentMethod(); // 输出: "Parent method" childInstance.childMethod(); // 输出: "Child method" ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

彩色之外

你的打赏是我创作的氮气加速动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值