抱歉这个愚蠢的问题 所以我在这里得到了这段代码:
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 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)
,则假设使用x
替换为hatchet
来执行此功能。但相反,我得到一个错误:“x未定义”。有人能帮我吗?
谢谢你的帮助。
答案 0 :(得分:1)
我假设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)
答案 1 :(得分:0)
它是@brk或@ D-reaper建议的
player.weaponATK = weapon.x.atk;
和类似的需要是
player.weaponATK = weapon[x].atk;
或只是
player.weaponATK = x.atk;
答案 2 :(得分:0)
由于x是传递给访问对象属性的密钥。它必须用作
的 weapon[x].atk
强>