<div class="codetitle"> <span><a style="CURSOR: pointer" data="98386" class="copybut" id="copybut98386" onclick="doCopy('code98386')"><u>複製程式碼</u></a></span> 程式碼如下:</div> <div class="codebody" id="code98386"> <br><script type="text/javascript"> <BR><! -- <BR>var my={ <BR>str:'', <BR>deep:0, <BR>block:' ', <BR>get_pre:function(n) <BR>{ <BR>pre= ''; <BR>for(i=0;i<n;i ) <BR>{ <BR>pre =this.block; <BR>} <BR>return pre; <BR>}, <BR>show_obj>show_obj>return pre; <BR>}, <BR>show_obj :function(obj) <BR>{ <BR>for(k in obj) <BR>{ <BR>if(typeof(obj[k])!='object' && typeof(obj[k])!=' array') <BR>{ <BR>pre=this.get_pre(this.deep); <BR>this.str =pre k '=>' obj[k] 'n'; <BR>} <BR>else if(typeof(obj[k])=='object' && typeof(obj[k].length)=='undefined')//如果是物件<BR>{ <BR>pre=this.get_pre(this. deep); <BR>this.str =pre k '=>OBJECT{n'; <BR>this.deep ;//開始遞歸,深度1 <BR>this.show_obj(obj[k]); <BR> pre = this.get_pre(this.deep); <BR>this.deep--;//遞歸結束一個深度-1 <BR>this.str =pre '}n'; <BR>} <BR>else if (typeof(obj[k])=='object' && typeof(obj[k].length)!='undefined')//如果是數組<BR>{ <BR>pre=this.get_pre(this.deep ); <BR>this.str =pre k '=>ARRAY[n'; <BR>this.deep ;//同物件<BR>this.show_obj(obj[k]); <BR>pre = this. get_pre(this.deep); <BR>this.deep--;//同物件<BR>this.str =pre ']n'; <BR>} <BR>} <BR>return this.str; <BR>}, <BR>alert_obj:function(obj) <BR>{ <BR>alert(this.show_obj(obj)) <BR>} <BR>} <BR>my.alert_obj({a:{b: {c:{d:'hello world'}}}}); <BR>//--> </script> </div> <br><br> chrome 可以用console.log ie的話,