我创建了一个方法" getDamage",它应该返回角色的基础伤害,或者他分配给它的武器的伤害(如果有的话)并给他增加基础伤害
function Weapon(name,damage) { // properties of weapons
this.name = name;
this.damage = damage;
}
Weapon.prototype.toString = function () {
return this.name + this.damage + " damage"; // method to show weapon
};
var bow = new Weapon('Golden bow, ', 20); // create weapon
function Character() {
this.health = 100;
this.basicDamage = 10;
this.toString = function () {
return this.name + this.weapon + this.heroDamage; // show hero his weapon and stack damage
};
Character.createArcher = function () { // create hero
var hero = new Character;
hero.name = "Legolas";
return hero;
};
Character.prototype.setWeapon = function (weapon) {
var me = this;
me.weapon = weapon; // contact with weapon
return me;
};
Character.prototype.getDamage = function (heroDamage) {
var stack = this.basicDamage + this.damage;
stack.heroDamage = heroDamage;
return stack;
};
var archer = new Character.createArcher();
archer.setWeapon(bow); // set the selected weapon
console.log(archer.toString());

答案 0 :(得分:1)
New edited complete answer
`
/*Weapon Constructor*/
function Weapon(name,damage) { // properties of weapons
this.name = name;
this.damage = damage;
}
/*Weapon Methods*/
Weapon.prototype.toString = function () {
return this.name + this.damage + " damage"; // method to show weapon
};
/*Character Constructor*/
function Character() {
this.health = 100;
this.basicDamage = 10;
this.toString = function () {
return this.heroName + this.weapon.name + this.heroDamage; // show hero his weapon and stack damage
};
}
/*Character methods
*/
/*Pass weapon object to this method to set weapon details for character*/
Character.prototype.setWeapon = function (weapon) {
this.weapon = weapon;
};
Character.prototype.createArcher = function (heroName) { // create hero
this.heroName = heroName;
};
Character.prototype.getDamage = function (weapon) {
this.heroDamage = this.basicDamage + weapon.damage;
return this.heroDamage;
};
var bow = new Weapon('Golden bow, ', 20);
bow.toString(); //output: "Golden bow, 20 damage"
var archer = new Character();
archer.createArcher('Legolas');
archer.setWeapon(bow);
archer.getDamage(archer.weapon); //output: 30
archer.toString();
//output: "LegolasGolden bow, 30"
`