预期
对不起,愚蠢的问题,我的函数的参数不工作。
所以我在这里得到这个代码:预期
function stat(x) {
player.ATK = 0; player.DEF = 0; player.DEX = 0; player.crit = 0; player.HP = 0;
player.weaponATK = weapon.x.atk;
player.weaponDEF= weapon.x.def;
player.weaponDEX = weapon.x.dex;
player.weaponHP = weapon.x.hp;
player.weaponCrit = weapon.x.crit;
player.ATK = player.baseATK + player.weaponATK;
player.DEF = player.baseDEF + player.weaponDEF;
player.DEX = player.baseDEX + player.weaponDEX;
player.HP = player.baseHP + player.weaponHP;
player.crit = player.baseCrit + player.weaponCrit;
}
而这里的整个VAR我使用:
var player = {
HP: 0,
baseHP: 100,
weaponHP: 0,
ATK: 0,
baseATK: 0,
weaponATK: 0,
DEF: 0,
baseDEF: 0,
weaponDEF: 0,
DEX: 0,
baseDEX: 0,
weaponDEX: 0,
crit: 0,
baseCrit: 5,
weaponCrit: 0,
level: 1,
currentEXP: 0,
expLeft: 10
};
var weapon = {
hatchet: {
atk: 2,
def: -1,
dex: 0,
hp: 0,
crit: 0
},
woodenSword: {
atk: 5,
dex: 0,
def: 0,
hp: 0,
crit: 0,
},
ironSword: {
atk: 10,
crit: 5,
dex: 0,
def: 0,
hp: 0
},
blade: {
atk: 25,
crit: 20,
dex: 10,
hp: 0,
crit: 0,
},
mace: {
atk: 30,
def: 5,
dex: -1,
hp: 0,
crit: 0,
},
battleAxe: {
atk: 50,
def: 5,
dex: 0,
hp: 0,
crit: 0,
},
broadSword: {
atk: 100,
def: 20,
dex: 0,
crit: 0,
hp: 0
},
woodenShield: {
atk: 0,
def: 10,
dex: 0,
hp: 0,
crit: 0,
},
spikeShield: {
def: 15,
atk: 5,
dex: 0,
crit: 0,
hp: 0,
},
bomb: {
atk: 0,
def: 0,
crit: 0,
hp: 0,
dex: -5
}
};
如果我跑stat(hatchet)
,它想EXCUTE与x
功能与hatchet
取代。但是,我得到一个错误:“x未定义”。有人能帮我吗? 感谢您的帮助。
我假设x是武器对象的属性,所以你可以使用括号符号从类似下面的武器来访问它。最后,当你调用stat函数时,确保它是一个字符串。请参见下面的工作代码:
var player = {
HP: 0,
baseHP: 100,
weaponHP: 0,
ATK: 0,
baseATK: 0,
weaponATK: 0,
DEF: 0,
baseDEF: 0,
weaponDEF: 0,
DEX: 0,
baseDEX: 0,
weaponDEX: 0,
crit: 0,
baseCrit: 5,
weaponCrit: 0,
level: 1,
currentEXP: 0,
expLeft: 10
};
var weapon = {
hatchet: {
atk: 2,
def: -1,
dex: 0,
hp: 0,
crit: 0
},
woodenSword: {
atk: 5,
dex: 0,
def: 0,
hp: 0,
crit: 0,
},
ironSword: {
atk: 10,
crit: 5,
dex: 0,
def: 0,
hp: 0
},
blade: {
atk: 25,
crit: 20,
dex: 10,
hp: 0,
crit: 0,
},
mace: {
atk: 30,
def: 5,
dex: -1,
hp: 0,
crit: 0,
},
battleAxe: {
atk: 50,
def: 5,
dex: 0,
hp: 0,
crit: 0,
},
broadSword: {
atk: 100,
def: 20,
dex: 0,
crit: 0,
hp: 0
},
woodenShield: {
atk: 0,
def: 10,
dex: 0,
hp: 0,
crit: 0,
},
spikeShield: {
def: 15,
atk: 5,
dex: 0,
crit: 0,
hp: 0,
},
bomb: {
atk: 0,
def: 0,
crit: 0,
hp: 0,
dex: -5
}
};
function stat(x) {
player.ATK = 0; player.DEF = 0; player.DEX = 0; player.crit = 0; player.HP = 0;
player.weaponATK = weapon[x].atk;
player.weaponDEF= weapon[x].def;
player.weaponDEX = weapon[x].dex;
player.weaponHP = weapon[x].hp;
player.weaponCrit = weapon[x].crit;
player.ATK = player.baseATK + player.weaponATK;
player.DEF = player.baseDEF + player.weaponDEF;
player.DEX = player.baseDEX + player.weaponDEX;
player.HP = player.baseHP + player.weaponHP;
player.crit = player.baseCrit + player.weaponCrit;
}
// make sure x is a string!
stat('hatchet');
console.log(player)
没有工作。我仍然有这个错误。 – AzDeveloper
是的,它的确如此。再次检查帖子 – AzDeveloper
将点符号仍然工作?或者括号notaion是必须的? – AzDeveloper
这是不是什么@brk或@ d-收割机建议
player.weaponATK = weapon.x.atk;
和类似需要作
player.weaponATK = weapon[x].atk;
或只是
player.weaponATK = x.atk;
因为X键通过访问该对象的属性。它必须用作 weapon[x].atk
什么是'hatchet'定义为? –
的可能的复制[动态访问对象的属性使用变量(https://*.com/questions/4244896/dynamically-access-object-property-using-variable) – Gerrit0
参数是'x'但您尝试访问'weapon.x' – brk