ES5实现继承---寄生组合式继承

核心思想:
创建一个中间对象,这个对象满足其隐式原型指向父类的显式原型,将这个对象赋值给子类的显式原型。  

    function createObj(o) {
      function F() {}
      F.prototype = o
      return new F()
    }
    function inherit(subType, superType) {
      subType.prototype = createObj(superType.prototype)
      Object.defineProperty(subType.prototype, 'constructor', {
        enumerable: false,
        configurable: true,
        writable: true,
        value: subType
      })
    }

    // 实现Student继承自Person
    function Person() {}
    function Student() {
      // 属性继承
      Person.call(this)
    }
    // 方法继承
    inherit(Student, Person)


#js#
全部评论
学习寄生组合式继承
点赞 回复 分享
发布于 2022-10-17 16:30 河南

相关推荐

07-08 21:30
已编辑
南昌大学 嵌入式软件开发
点赞 评论 收藏
分享
07-24 16:39
已编辑
门头沟学院 测试开发
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务