我试图将不同对象的多个实例添加到另一个对象的数组。但是,我遇到了麻烦。

*Creates the player*
function Player(){
    this.name = "";
    this.status = "Alive";
    this.primaryClass = null;
    this.secondaryClass = null;
    this.strength = 0;
    this.stamina = 0;
    this.mystica = 0;
    this.health = 0;
    this.primaryWeapon = null;
    this.offHand = null;
    this.accuracy = 0;
    this.block = 0;
    this.baseDamage = 0;
    this.maxDamage = 0;
    this.attackSpeed = 0;
    this.shield = null;
    this.armor = null;
    this.armorRating = 0;
    this.exp = 0;
}
*Creates the sword weapon*
function LongSword(){
    this.name = "";
    this.attackSpeed = 1;
    this.baseDamage = 10;
    this.maxDamage = 15;
    this.durability = 100;
    this.block = 5;
    this.exp = 0;
}
*Creates the Long Sword skills*
function Stab(){
    this.name = "Stab";
    this.status = "Unlocked";
    this.damage = 0;
    this.damageModifier = 0.75;
    this.cooldown = 5;
    this.exp = 0;
    this.desc = "Stabs your opponent for an additional " +parseInt(this.damageModifier * 100) +"% of your base damage.";
}
*Equips the Player weapon(s)*
Player.prototype.equipWeapon = function equipWeapon(main, offHand){
    if(this.primaryClass.dualWield){
        this.primaryWeapon = main;
        if(offHand){
            this.offHand = offHand;
            this.baseDamage += (this.strength + (main.baseDamage + (offHand.baseDamage / 2))) / 10;
            this.maxDamage += (this.strength + (main.maxDamage + (offHand.maxDamage / 2))) / 5;
            this.attackSpeed += main.attackSpeed + (offHand.attackSpeed / 2);
            this.block += main.block + offHand.block;
        }
    }
    else{
        this.primaryWeapon = main;
        this.offHand = null;
        this.baseDamage += (this.strength + main.baseDamage) / 10;
        this.maxDamage += (this.strength + main.maxDamage) / 5;
        this.attackSpeed += main.attackSpeed;
        this.block += main.block;
    }

    if(!this.primaryClass.dualWield && offHand){
        console.log("Your class can not wield dual weapons.");
    }
}
*Equips the Weapon skills*
LongSword.prototype.skills = function skills(skill){
    this.skills = [];
    skill.damage = parseFloat((this.baseDamage / skill.damageModifier).toFixed(1));
    this.skills.push(skill);
}


这些对象构成了我正在尝试做的基本元素。所以当我实例化每个实例时,

var Robert = new Player();
Robert.equipWeapon(new LongSword());
Robert.primaryWeapon.skills(new Stab());


我得到了想要的结果。但是,如果我尝试添加Stab()的另一个实例,以使它看起来像这样

var Robert = new Player();
Robert.equipWeapon(new LongSword());
Robert.primaryWeapon.skills(new Stab());
Robert.primaryWeapon.skills(new Stab());


我收到TypeError:Robert.primaryWeapon.skills is not a function。为什么一次只能正常工作,而第二次却不能正常工作。我试图达到的最终结果是,如果将Robert.primaryWeapon.skills调出,我应该看到Stab对象的两个实例。

最佳答案

Longsword的原型中有两个问题。

首先,用存储的skill数组替换函数,它们具有相同的名称:

LongSword.prototype.skills = function skills(skill){
    this.skills = []; //Overrides your function
    ...


导致错误Robert.primaryWeapon.skills is not a function的原因是,一旦调用它,它确实是一个数组。

要修复它,只需更改函数或数组之一的名称。

其次,您每次调用该函数都会将skills数组初始化为一个空数组,每次都将其重置。您应该在Longsword的原型中对其进行初始化。



这是使用这些修复程序(fiddle with it if you want)的示例:

function LongSword(){
   this.skills = [];
   ...

LongSword.prototype.addSkill = function skills(skill){
    ...


然后,您将能够添加多种技能:

var Robert = new Player();
Robert.equipWeapon(new LongSword());
Robert.primaryWeapon.addSkill(new Stab());
Robert.primaryWeapon.addSkill(new Stab());

关于javascript - 将一个对象的多个实例添加到另一个对象的数组中;,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38675100/

10-12 12:40
查看更多