模仿console自寫函數打印js的對象


本以為寫個遞歸函數就可以將js的對象打印出來。

當然第一個想到的估計是JSON.stringify() 這個函數。但這個函數打印到瀏覽器 顯示效果不友好。最友好的顯示肯定是 控制台打印咯。

結果嘗試打印window的時候,直接掛逼。原因就是對象循環引用。

經過幾次修改,還是禁止了window里的某幾個屬性。

 

 1 function parseObjToString(obj){
 2     var filter = ['top', 'window', 'document', 'localStorage', 'sessionStorage', 'cookie'], //不會解析該屬性。
 3      x         = null, //沒用的臨時變量
 4      n         = '\n<br/>', //換行
 5      Parse     = {
 6         parse_obj : function(obj, blank, parse_obj_times){//解析對象和數組
 7             !parse_obj_times && (parse_obj_times = 0);
 8             if (parse_obj_times > 20) {return '';}
 9             switch(typeof obj){
10                 case 'object':
11                     var temp   = '{',
12                         isobj  = true;
13                         temp  += n;
14                         blank  = blank || 1;
15                     ''+{}.toString.call(obj) === "[object Array]" && (isobj = false);
16                     isobj ? temp = '{'+ n : temp = '['+ n;
17                     for (x in obj) {
18                         if (typeof obj[x] == 'object') {
19                             if(filter.indexOf(x) !== -1 ) continue;
20                             parse_obj_times ++;
21                             temp += this.get_blank(blank)+ x+ ' : '+ this.parse_obj(obj[x], blank+1, parse_obj_times)+ ',' + n;
22                         }else{
23                             temp += this.get_blank(blank)+ x+ ' : '+ this.parse_sign(obj[x]) +',' + n;
24                         }
25                     }
26                     temp = temp.substr(0, temp.length - (',' + n).length)+ n;
27                     return temp + this.get_blank(blank - 1) + (isobj ? '}' : ']') ;
28                 default : 
29                   return obj;
30             }
31         },
32         parse_sign: function(str){//解析特殊符號
33             return (''+str).replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/\n|\n\r/g, '<br>')
34                            .replace(/\t/g, '&nbsp;&nbsp;').replace(/\s\s/g, '&nbsp;');
35         },
36         get_blank : function(num){//返回相應num的空白
37             for (var i = 0, blank=''; i < num; i++) {
38                 blank += '&nbsp;&nbsp;&nbsp;&nbsp;';
39             }
40             return blank;
41         }
42     };
43     return Parse.parse_obj(obj);
44 }

 

測試數據:

 1 var obj = {
 2         test : {
 3             a : 'hello',
 4             b : 'world',
 5             c : {
 6                 d : {
 7                     e: {
 8                         f: {
 9                             g: {
10                                 h : {
11                                     i : {
12                                         shit : 'yes shit'
13                                     }
14                                 }
15                             }
16                         }
17                     }
18                 }
19             }
20         },
21         arr : [
22             'what',
23             'a',
24             'abc',
25             '<aaa><bbb>',
26             'a<asdfsafd>'
27         ],
28         func : function(e){
29             console.log('aaaa');
30         },
31         func2: function(e){
32             console.log('bbbb');
33         },
34         func3: function(e){
35             console.log('asdfs');
36         }
37     };
38 
39 var str = parseObjToString(obj);;
40 document.write(str);

測試結果:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM