这是很基本的...
我坚持要做什么。
alert("The capital of " + n + " is " + capitals.n);
警报中的 capitals.n 显示为未定义。我能做些什么来解决这个问题?
这是很基本的...
我坚持要做什么。
alert("The capital of " + n + " is " + capitals.n);
警报中的 capitals.n 显示为未定义。我能做些什么来解决这个问题?
使用方括号:
alert("The capital of " + n + " is " + capitals[n]);
您当前所拥有的将查找不存在的capitals带有 identifier的属性。n相反,您希望使用 的值作为n标识符。
使用方括号代替点表示法:
alert("The capital of " + n + " is " + capitals[n]);
解释:
capitals.n查找字面上名为“n”的属性。capitals[n]查找以变量值n作为名称的属性。capitals.n(通过在您的代码中给出一个值来验证,例如capitals.n = 'FOO':)