我想缩短这些行
var upgradethis = "u3";
if (nev == "u1"){
mM.u1.count.text=minionCounter[lvl];
}
if (nev == "u2"){
mM.u2.count.text=minionCounter[lvl];
}
如何将“u1”和“u2”替换为“upgradethis”的值?
在这里使用:
mM。升级此值 .count.text = minionCounter [lvl];
(将此代码中的u2替换为“upgradethis”的值)
答案 0 :(得分:3)
根据Question with no answers, but issue solved in the comments (or extended in chat):
使用mM[upgradethis].count.text
解决问题。
归因于Cherniv, who posted it here的解决方案。
请注意,如果您不检查属性是否存在,则可以遇到TypeError #1010: A term is undefined and has no properties
。您可以使用hasOwnProperty
(语法if(mm.hasOwnProperty(upgradethis))
)。