账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    用组合继承写一个继承,获取不到父类原型链上的方法
    41
    0

    题目描述

    我想写一个继承,子类的实例能够继承父类的方法和属性

    题目来源及自己的思路

    相关代码

    function Animal (name) {
        this.name = name || 'Animal';
        this.sleep = function () {
            console.log(this.name + '正在' + 'eatting')
        }
    }
    Animal.prototype.eat = function (food) {
        console.log(this.name +'吃' + food)
    }
    
    function Cat(name, food) {
        Animal.call(this, name)
        // console.log(Animal.name, name)
        this.food = food || 'meat'
    }
    extend (Animal, Cat)
    function extend (subClass, superClass) {
        subClass.prototype = new superClass()
        subClass.prototype.constuctor = subClass //修复构造函数指向的
    }
    const cat = new Cat('haixin', 'fish')
    console.log(cat.name) // haixin
    console.log(cat) // Cat { name: 'haixin', sleep: [Function], food: 'fish' }
    //cat上面没有Animal上原型链上的eat方法
    cat.eat() // TypeError: cat.eat is not a function

    你期待的结果是什么?实际看到的错误信息又是什么?

    我以为新生成的cat这个实例能够获得Cat和Animal上的方法和属性,但Animal上原型链上的eat方法获取不到,不知道为什么?还有为什么extend方法里subClass.prototype的constuctor属性要重新指向subClass?

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 你爱笑 普通会员 1楼

      在JavaScript中,组合继承通常用于构建具有父类属性和方法的子类,但获取父类原型链上的方法通常涉及对象的实例化和原型链的访问。下面是一个例子,展示如何在组合继承中获取父类原型链上的方法:

      ```javascript // 基类 class Base { // 基类方法 sayHello() { console.log('Hello from the base class'); } }

      // 子类 class Child extends Base { constructor() { super(); this.myMethod = function() { console.log('Hello from the child class'); }; }

      // 使用组合继承获取父类原型链上的方法 getSuperMethod() { return { sayHello() { console.log('Hello from the super class'); } }; }

      // 创建子类实例并调用方法 static createChildInstance() { const child = new Child(); child.sayHello(); // 输出:Hello from the child class } }

      // 调用组合继承获取父类原型链上的方法 Child.createChildInstance(); ```

      在这个例子中,Child类继承自Base类,并添加了一个名为myMethod的新方法。getSuperMethod方法是通过组合继承来获取父类原型链上的sayHello方法。首先,getSuperMethod方法通过super关键字创建一个包含sayHello方法的实例,然后返回这个实例。然后,Child类的实例可以通过Child.createChildInstance方法创建,并调用myMethod方法,输出:Hello from the child class

      注意,getSuperMethod方法返回的是一个包含sayHello方法的实例对象,而不是直接返回sayHello方法本身。这是因为在组合继承中,子类继承父类的方法并添加了自己的方法,因此子类方法的实现通常会覆盖父类方法的实现,即不会返回父类方法本身。如果你需要获取子类方法本身,可以将getSuperMethod方法返回值转换为一个包含子类方法方法名和方法实现的JavaScript对象,然后通过Object.getPrototypeOf方法来获取原型链上的方法对象。例如:

      ```javascript // 子类 class Child extends Base { constructor() { super(); this.myMethod = function() { console.log('Hello from the child class'); }; }

      // 使用组合继承获取父类原型链上的方法 getSuperMethod() { return { sayHello() { console.log('Hello from the super class'); }, myMethod() { console.log('Hello from the child class'); } }; }

      // 创建子类实例并调用方法 static createChildInstance() { const child = new Child(); child.sayHello(); // 输出:Hello from the child class const methodObject = Object.getPrototypeOf(child.getSuperMethod()); // 获取子类方法对象 methodObject.sayHello(); // 输出:Hello from the child class } }

      // 调用组合继承获取父类原型链上的方法 Child.createChildInstance(); ```

      在这个例子中,getSuperMethod方法返回的对象methodObject包含了子类方法myMethod的实现,通过Object.getPrototypeOf方法可以获取到myMethod方法的原型链。myMethod方法实际上是在Child类中定义的,因此可以直接调用myMethod方法。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部