/*枚举为什么我会得到不同的结果?使用name.property和name [property]
for in语句可以遍历对象中的所有属性名称。枚举将包含函数和原型属性。 */
//第一个代码我写
var fruit = {
apple: 2,
orange: 5,
pear:1
},
sentence = 'I have',
quantity;
for (kind in fruit) {
quantity = fruit[kind];
sentence += quantity + '' + kind +
(quantity === 1?'': 's') +
', ';
}
sentence = sentence.substr(0,sentence.length-2) + '.';
alert(sentence);
//第二个代码我写
var fruit = {
apple: 2,
orange: 5,
pear:1
},
sentence = 'I have',
quantity;//
for (kind in fruit) {
quantity = fruit.kind;
sentence += quantity + '' + kind +
(quantity === 1?'': 's') +
', ';
}
sentence = sentence.substr(0,sentence.length-2) + '.';
alert(sentence);
因为fruit.kind等于水果。['kind']。你的第二个例子中没有评价类。 – Bellian
语法错误:'fruit。['kind']'实际上应该是'fruit ['kind']' –