for...in语句: 用于遍历对象属性。
用for...in遍历"数组": 循环变量x是数组的下标
///////用for...in遍历"数组": 循环变量x是数组的下标 //////////
var arr=[22,33,44,55,66];
for(x in arr){
//Java中的x是数组元素,而js中的x是数组下标
//document.write(x+" "); //下标 : 0 1 2 3 4
document.write(arr[x]+" "); //元素
}
document.write("<br/> <hr/>");
用for...in遍历"js对象": 循环变量x是json的成员---属性与方法名
<script type="text/javascript">
、
///////用for...in遍历"js对象": 循环变量x是json的成员---属性与方法名 //////////
function Person(name, age){
this.name = name;
this.age = age;
this.toString = function(){
return this.name + "," + this.age;
}
}
var p = new Person("Jack",22);
for(x in p){
//document.write(x+" "); //属性与方法名: name age toString
//document.write(p.x+" ");
document.write(p[x]+" "); //属性值与方法体
}
document.write("<br/>");
</script>