var arr=['趙','錢','孫','李'];
for(var i=0;i<arr.length;i++){
console.log(arr[i]);
}web
var obj={
name:'li',
age:20,
area:'ni' }
for(var k in obj){
console.log(k+'~'+obj[k])
}chrome
for in語句循環會枚舉對象的每個屬性session
console.log(k)打印的是每個鍵,即name,age,area對象
console.log(k+'~'+obj.k)打印obj.k是對象的k屬性,可是咱們要把k的值當作obj的屬性,這樣就產生歧義了,因此打印出來是undefinedit
console.log(k+'~'+obj[k])打印出來是name~li age~20 area~niio
console.log(k+'~'+obj['k'])若是想要得到obj名稱叫作‘k’的屬性,就能夠這樣來寫,此時打印出來是undefined,由於obj沒有這樣的屬性值console
console.log(k+'~'+obj['age'])由於obj有這樣的屬性值,所以打印出來是name~20 age~20 area~20循環
for in語句循環會枚舉對象的每個屬性webkit
例如 vi
var window;
for(var k in window){
console.log(k);
}
打印
external
chrome
document
k
speechSynthesis
caches
localStorage
sessionStorage
webkitStorageInfo
indexedDB
webkitIndexedDB
ondeviceorientation
……