Javascript 如何使用“设置新属性”;“设置”;方法

Javascript 如何使用“设置新属性”;“设置”;方法,javascript,Javascript,我想通过创建“武器”属性来设置武器的“设置武器”。示例调用:“archer.setarm(bow);” 功能武器(名称、伤害){ this.name=名称; 这个。损坏=损坏; } wearm.prototype.toString=函数(){ 返回this.name+this.damage+“damage”//显示武器的方法 }; var弓=新武器('金弓',20);//制造武器 函数字符(){ 这是健康=100; 这个。基本损伤=10; this.toString=函数(){ 返回this.

我想通过创建“武器”属性来设置武器的“设置武器”。示例调用:“archer.setarm(bow);”

功能武器(名称、伤害){
this.name=名称;
这个。损坏=损坏;
}
wearm.prototype.toString=函数(){
返回this.name+this.damage+“damage”//显示武器的方法
};
var弓=新武器('金弓',20);//制造武器
函数字符(){
这是健康=100;
这个。基本损伤=10;
this.toString=函数(){
返回this.name+this.type;//添加并显示武器弓
};
Character.createArcher=函数(){//创建英雄
英雄=新角色;
hero.name=“Legolas”;
回归英雄;
};
Character.setarm=函数(){
var武器=新角色();
武器类型=武器;
返回武器;
};
var archer=new Character.createArcher();
弓箭手。设置武器(弓);//设置所选武器

console.log(archer.toString());
一种方法是用setarm扩展角色原型,setter只设置值-它们不返回值。你可以为此编写getarm()。这样你就可以传递实际的武器并分配给射手。希望能有所帮助。(武器是一个对象,所以你可以访问archer.armor.type)


未捕获类型错误:archer.setArmover不是函数:(
var bow = new Weapon('Golden bow, ', 20); // create weapon 

Character.prototype.setWeapon = function (weapon) { 
    var me = this;
    me.weapon = weapon; // set the weapon
};

var archer = new Character.createArcher();
archer.setWeapon(bow); // set the selected weapon
console.log(archer.toString()); // add to string also property weapon