- 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 条
- 全部回答
-
你爱笑 普通会员 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方法。
- 扫一扫访问手机版
回答动态

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器更新之后。服务器里面有部分玩家要重新创建角色是怎么回事啊?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题函数计算不同地域的是不能用内网吧?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题ARMS可以创建多个应用嘛?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题在ARMS如何申请加入公测呀?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题前端小程序接入这个arms具体是如何接入监控的,这个init方法在哪里进行添加?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器刚到期,是不是就不能再导出存档了呢?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器的游戏版本不兼容 尝试更新怎么解决?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器服务器升级以后 就链接不上了,怎么办?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器转移以后服务器进不去了,怎么解决?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器修改参数后游戏进入不了,是什么情况?预计能赚取 0积分收益
- 回到顶部
- 回到顶部

