/*枚舉爲什麼我會得到不同的結果?使用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']' –