3

在Firebug中,您可以將DOM選項卡的輸出設置爲僅顯示用戶定義的功能和屬性。這對於檢查是否有對象轉義到全局名稱空間很有幫助。 Chrome中是否有相同的內容?是否可以在Google Chrome控制檯中只顯示用戶定義的功能和屬性?

+0

[這個問題](http://stackoverflow.com/questions/6308955/is-there-a-way-to-get-document-and-window-properties-in-chrome/ 6311663#6311663)是相似的,但我特別尋找一種方法來過濾窗口對象 – darronz 2013-02-20 10:47:33

回答

0

這裏有一個非常近似:

控制檯版本:

var current; 
for(current in window) 
    { 
    /* If the property is not null or undefined */ 
    if (!!window[current]) 
    { 
    /* If the constructor is the Function object */ 
    if (/Function/.test(String(window[current].constructor))) 
     { 
     /* Print to the console */ 
     console.log(current) 
     } 
    } 
    } 

小書籤版本:

javascript:void(function(){for(_ in window) { if (!!window[_]) { if (/Function/.test(String(window[_].constructor))) { console.log(_) } } }}()) 

通用版本:

function getUDFs() 
{ 
var current; 
/* Use current instead of _ to avoid creating an iterator global variable */ 
for(current in arguments[0]) 
    { 
    /* If the property is not null or undefined */ 
    if (!!arguments[0][current]) 
    { 
    /* If the constructor is the Function object */ 
    if (/Function/.test(String(arguments[0][current].constructor))) 
     { 
     /* Print to the console */ 
     console.log(current) 
     } 
    } 
    } 
} 

遞歸版本:

function getUDFs() 
{ 
var current; 
/* Use current instead of _ to avoid creating an iterator global variable */ 
for(current in arguments[0]) 
    { 
    getUDFs.id = arguments[1] + " => "; 
    /* If the property is not null or undefined */ 
    if (!!arguments[0][current]) 
    { 
    /* If the constructor is the Function object */ 
    if (/Function/.test(String(arguments[0][current].constructor))) 
     { 
     /* Print to the console */ 
     console.log(getUDFs.id + current) 
     } 

    /* Check object properties */ 
    if (/Object/.test(String(arguments[0][current].constructor))) 
    { 
    getUDFs(arguments[0][current], getUDFs.id + current) 
    } 

    /* Check prototype properties, but skip constructor prototypes */ 

    if (!!arguments[0][current] && arguments[0][current].hasOwnProperty("prototype") && arguments[0][current] !== arguments[0]["constructor"]) 
    { 
    getUDFs(arguments[0][current]["prototype"], getUDFs.id + current + " => prototype") 
    } 
    } 
    } 
} 
getUDFs(jQuery,"jQuery") 

遞歸版本與存儲:

function getUDFs() 
    { 
    var current; 

    /* Use current instead of _ to avoid creating an iterator global variable */ 

    for(current in arguments[0]) 
     { 
     getUDFs.id = arguments[1] + " => "; 

     /* If the property is not null or undefined */ 
     if (!!arguments[0][current]) 
     { 
     /* If the constructor is the Function object */ 
     if (/Function/.test(String(arguments[0][current].constructor))) 
      { 
      /* Store in an array */ 
      if (getUDFs.hasOwnProperty("data")) 
      { 
      getUDFs.data.push(getUDFs.id + current) 
      } 
      else 
      { 
      getUDFs.data = [] 
      } 
      } 

     if (/Object/.test(String(arguments[0][current].constructor))) 
     { 
     getUDFs(arguments[0][current], getUDFs.id + current) 
     } 
     } 
     } 
    } 
    getUDFs(jQuery,"jQuery")  

遞歸版本與取證:

function getUDFs() 
{ 
var current; 

/* Use current instead of _ to avoid creating an iterator global variable */ 

for(current in arguments[0]) 
    { 
    getUDFs.id = arguments[1] + " => "; 

    /* If the property is not null or undefined */ 
    if (!!arguments[0][current]) 
    { 
    /* If the constructor is the Function object */ 
    if (/Function/.test(String(arguments[0][current].constructor))) 
     { 
     /* Store in an array */ 
     if (getUDFs.hasOwnProperty("data")) 
     { 
     try{getUDFs.data.push(getUDFs.id + current + String().concat("- args: ","(", arguments[0][current]["length"], ")"))}catch(e){getUDFs.data.push(getUDFs.id + current)}; 

     try{getUDFs.data[getUDFs.data.length-1] += "required:" + !arguments[0][current]() ? true: false}catch(e){getUDFs.data[getUDFs.data.length-1] += "required: true"} 
     } 
     else 
     { 
     getUDFs.data = [] 
     } 
     } 

    if (arguments[0].hasOwnProperty(current)) 
     { 
     if (/Object/.test(String(arguments[0][current].constructor))) 
     { 
     getUDFs(arguments[0][current], getUDFs.id + current) 
     } 
     } 
    } 
    } 
} 
getUDFs(jQuery,"jQuery"); 
getUDFs.data.toString().replace(",","\n","g") 

參考

+1

這會將變量泄漏到全局範圍。這不是一個很好的近似值。 'Object.keys(window)'更容易輸入,並且相當接近。 – 2013-08-26 21:09:48

+0

對於發佈的近似值,我獲得了167個節點,ECMAScript-5 Object.keys方法獲得了205個節點。無論如何,這篇文章是社區維基的原因,所以隨時編輯。 – 2013-08-26 21:33:27

相關問題