在实际工作中,记住浏览器中对象的属性、方法几乎是件不可能完成的任务,保持一份资料或网址是个好办法。但是查阅资料也是要花费不少时间,如果能有个脚本将对象的结构打印出来,将会加速开发进程。
下面是我编写的查看对象结构例子,请下载附件获取可执行代码:
- //Animal是一个对象
- Animal = {
- createNew: function( bundle ) {
- var animal = {};
- var protect = bundle || {};//bundle传递的是指针,修改protect时外面对象会跟着变(除非不传)
- protect.sound = 'growl';
- protect.makeSound = function(){
- return protect.sound;
- }
- return animal;
- }
- }
- //Cat也是一个对象
- Cat = {
- pubVar:"Cat",
- 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;
- }
- }
- //javascript区分大小写,通过Cat对象构造一个新的对象赋值给cat
- var cat = Cat.createNew("meow!");
- var bigCat = Cat.createNew("meow!meow!meow!");
- showObject("Cat");
- showObject("cat");
- 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键。