离线下载
PDF版 ePub版

xinqiu · 更新于 2018-11-28 11:00:43

枚举

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')+
                ', ';
}
 // The following line removes the trailing coma.
sentence = sentence.substr(0,sentence.length-2)+'.';
 // I have 2 apples, 5 oranges, 1 pear.
上一篇: 销毁 下一篇: 全局化