js查看Object对象的内容(转)
假设将对象赋给test,此时test是也是对象。
var test = object;
一、查看对象内容(一级对象)。
for(i in test ){
alert(i); //获得属性
alert(test[i]); //获得属性值
}
二、查看对象里的对象(二级及以上)
for(i in test ){
alert(i);
alert(test[i].toSource());
}
附:
使用JS的 for...in语句
--不知属性个数时,用于对某个对象的所以属性进行循环操作,返回字符串形式的属性名;获取属性值方式: p1[temp] ;
跟Java的for(Student stu:students){...}很像;
语法: for(临时变量 in 对象实例名){
...
}
function Person(){
this.name="bai";
this.age=19;
this.height=175;
}
var p1= new Person();
var temp,p1Str="";
for(temp in p1){
alert(typeof temp); //是字符串
alert(p1[temp]);
p1Str=p1Str+temp+" -- "
}
alert(p1Str); //返回"name -- age -- height"
for (var obj in x)
{
document.write("<p>属性:"+obj+"=内容:"+eval("x."+obj)+"</p>");
}