ages = {paul: 11, rick: 7 }
for n,a in ages
console.log("names: #{n}, ages: #{a}")
因此控制台中没有打印任何内容。我做错了什么?
答案 0 :(得分:3)
转换为以下JavaScript:
var a, ages, n, _i, _len;
ages = {
paul: 11,
rick: 7
};
for (a = _i = 0, _len = ages.length; _i < _len; a = ++_i) {
n = ages[a];
console.log("names: " + n + ", ages: " + a);
}
请注意,它正在尝试使用ages
属性循环遍历length
对象,该属性不存在。
更改您的脚本,以便循环使用for n,a
of
ages
语法代替(relevant documentation),并将您的代码转换为有效的JavaScript和打印到控制台。
var a, ages, n;
ages = {
paul: 11,
rick: 7
};
for (n in ages) {
a = ages[n];
console.log("names: " + n + ", ages: " + a);
}