可以从Javascript中的多个原型继承吗?

可以从Javascript中的多个原型继承吗?,javascript,Javascript,我想制作一个从两个不同原型继承的原型。我知道我可以通过对象连接或在构造函数函数中创建方法轻松实现这一点,但我希望将函数保存在原型链中 假设我有人类和机器人,我想创建一个新类型机器人: 功能人员(姓名,hp){ this.hp=hp | | 100 this.name=name } Human.prototype.eat=函数(){ 这是0.hp+=50 console.log(`current hp:${this.hp}`) } 功能机器人(名称、燃料){ this.fuel=fuel | |

我想制作一个从两个不同原型继承的原型。我知道我可以通过对象连接或在构造函数函数中创建方法轻松实现这一点,但我希望将函数保存在原型链中

假设我有
人类
机器人
,我想创建一个新类型
机器人

功能人员(姓名,hp){
this.hp=hp | | 100
this.name=name
}
Human.prototype.eat=函数(){
这是0.hp+=50
console.log(`current hp:${this.hp}`)
}
功能机器人(名称、燃料){
this.fuel=fuel | | 100
this.name=name
}
Robot.prototype.refuel=函数(){
这是燃油+=25
console.log(`current fuel:${this.fuel}`)
}
我如何将原型组合成一个,并使
机器人人
继承
人类
机器人

功能机器人人(姓名、马力、燃油){
Human.call(此、名称、hp)
机器人。呼叫(这个、名字、燃料)
}
RobotMan.prototype=Object.create(Human.prototype)/?我该如何正确地写这行?
RobotMan.prototype.constructor=RobotMan
您可以使用方法。此方法用于将自己的可枚举属性从一个或多个源复制到对象

assign
方法返回新对象,我已将该对象指定为
RobotMan
prototype

功能人员(姓名,hp){
this.hp=hp | | 100;
this.name=名称;
}
Human.prototype.eat=函数(){
这是1.hp+=50;
log(`current hp:${this.hp}`);
};
功能机器人(名称、燃料){
this.fuel=fuel | | 100;
this.name=名称;
}
Robot.prototype.refuel=函数(){
这是燃料+=25;
log(`current fuel:${this.fuel}`);
};
功能机器人人(姓名、马力、燃油){
Human.call(这个,名字,hp);
机器人。呼叫(这个,名字,燃料);
}
RobotMan.prototype=Object.assign({},Human.prototype,Robot.prototype);
const robotMan=新机器人人(“机器人人”,50,40);
机器人人吃东西;

机器人加油“我知道我可以通过对象连接轻松实现这一点”问题中是否缺少一些东西,因为
RobotMan.prototype=object.assign({},Human.prototype,Robot.prototype)
似乎适合您的用例?这是否回答了您的问题@弗拉兹,我试过了,以前不管用,但现在管用了。我想我在代码中的某个地方犯了错误。谢谢你的帮助!我只是意识到这不是正确的答案。这样做只会在调用
Object.assign
时复制并拍摄
Human.prototype
Robot.prototype
的快照。如果我在
Object.assign()
之后变异
Robot.prototype
RobotMan.prototype
将不会更改。