for...in可以遍历对象、数组。对于对象,返回所有的属性名;对于数组,返回索引。
var 水浒传 = ['宋江', '李逵', '武松', '林冲'];
var obj = { name: '孙悟空', age: 1000, gender: '男' };//返回name、age、gender
function Person(name, age, gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
var person = new Person('孙悟空', 2000, '男');
for (var n in 水浒传) {//返回0123;
console.log(n);
}
for (var n in obj) {//返回name, age, gender;
console.log(n);
}
for (var n in person) {//返回name, age, gender;
console.log(n);
}