在实际工作中,记住浏览器中对象的属性、方法几乎是件不可能完成的任务,保持一份资料或网址是个好办法。但是查阅资料也是要花费不少时间,如果能有个脚本将对象的结构打印出来,将会加速开发进程。

下面是我编写的查看对象结构例子,请下载附件获取可执行代码

 
  
  1. //Animal是一个对象  
  2. Animal = {  
  3.     createNew: function( bundle ) {  
  4.         var animal = {};  
  5.         var protect = bundle || {};//bundle传递的是指针,修改protect时外面对象会跟着变(除非不传)  
  6.         protect.sound = 'growl';  
  7.         protect.makeSound = function(){   
  8.             return protect.sound;   
  9.         }  
  10.         return animal;  
  11.     }  
  12. }  
  13.  
  14. //Cat也是一个对象  
  15. Cat = {  
  16.     pubVar:"Cat",  
  17.     createNew: function(mySound) {  
  18.         var protect = {};  
  19.         var cat = Animal.createNew( protect );//protect会被修改,然后返回空白对象{}给cat  
  20.         protect.sound = mySound;  
  21.         cat.meow = function(){ return protect.makeSound(); };//cat要调用protect中的方法  
  22.         return cat;  
  23.     }  
  24. }  
  25.    
  26. //javascript区分大小写,通过Cat对象构造一个新的对象赋值给cat  
  27. var cat = Cat.createNew("meow!");  
  28. var bigCat = Cat.createNew("meow!meow!meow!");  
  29.  
  30. showObject("Cat");  
  31. showObject("cat");  
  32. showObject("bigCat"); 

调试信息: 
    
    [Object] Cat
        |--[function] createNew = function(mySound) { var protect = {}; var cat = Animal.createNew( protect );//protect会被修改,然后返回空白对象{}给cat protect.sound = mySound; cat.meow = function(){ return protect.makeSound(); };//cat要调用protect中的方法 return cat; }
        |--[string] pubVar = Cat
    
    [Object] cat
        |--[function] meow = function(){ return protect.makeSound(); }
    
    [Object] bigCat
        |--[function] meow = function(){ return protect.makeSound(); }

[备注1]遍历DOM结构类似,例如后面将写一篇博文展示一下IE10的window对象。具体就是在IE10中执行showObject("window");

[备注2]object类型有下钻链接,但是有个小问题:不能回退,此时建议按F5键。