在實際工作中,記住瀏覽器中對象的屬性、方法幾乎是件不可能完成的任務,保持一份資料或網址是個好辦法。但是查閱資料也是要花費不少時間,如果能有個指令碼將對象的結構列印出來,將會加速開發進程。
下面是我編寫的查看對象結構例子,請下載附件擷取可執行代碼:
//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鍵。
本文出自 “iData” 部落格,請務必保留此出處http://idata.blog.51cto.com/4581576/1103408