2011-03-18 76 views
85

如何檢查警報框中的對象?通常提醒一個對象只是拋出nodename:如何檢查Javascript對象

alert(document); 

但我想要在警告框中的對象的屬性和方法。如果可能,我怎樣才能實現這個功能?或者還有其他建議嗎?

特別是,我正在爲console.log和Firebug不可用的生產環境尋求解決方案。

+9

做'在Firefox或Chrome – kjy112 2011-03-18 20:24:15

+1

我很困惑console.log'。在生產環境中,你有實際的用戶,對吧?那麼,爲什麼你要用對象屬性拋出一個警報?也許更好的解決方案是序列化對象並將其記錄在文件中或發送電子郵件? – 2011-03-19 00:17:28

+0

也許他/她需要警報作爲工具,但是需要實際的功能來做其他事情。可能有各種各樣的原因,爲什麼他/她可能想要這樣做,比如顯示統計數據或發生錯誤,或者通過簡單地將對象傳遞給用來檢查對象的任何對象。 – Christian 2011-03-21 00:06:25

回答

54

for - in爲對象或數組中的每個屬性循環。您可以使用此屬性獲取值並更改它。

注意:私人財產不可用於檢查,除非你使用「間諜」;基本上,你重寫對象並編寫一些代碼,在對象的上下文中執行for-in循環。

對於模樣:

for (var property in object) loop(); 

一些示例代碼:

function xinspect(o,i){ 
    if(typeof i=='undefined')i=''; 
    if(i.length>50)return '[MAX ITERATIONS]'; 
    var r=[]; 
    for(var p in o){ 
     var t=typeof o[p]; 
     r.push(i+'"'+p+'" ('+t+') => '+(t=='object' ? 'object:'+xinspect(o[p],i+' ') : o[p]+'')); 
    } 
    return r.join(i+'\n'); 
} 

// example of use: 
alert(xinspect(document)); 

編輯:前段時間,我寫我自己檢查,如果你有興趣,我樂意分享。

編輯2:好吧,我寫了一個。

+0

應該以某種方式保護遞歸。散列(*字典*)與一些內部的html渲染器id?對於noobs將此檢查插入代碼可能會有用。 – Nakilon 2012-04-03 06:58:27

+0

@Nakilon我總是遇到無限遞歸的問題,特別是在PHP中。有兩種方法可以解決這個問題:使用深度參數,或修改散列並添加您用於檢查遞歸的屬性。深度可能更安全。 – Christian 2012-04-03 07:08:31

+0

我更喜歡這個版本的第7行。它看起來更像紅寶石,並做了一些漂亮的空白 r.push(i +'''+ p +'「=>'+(t =='object'?'{\ n'+ xinspect(o [p] ,i +'')+('\ n'+ i +'},'):o [p] +'')); – 2012-06-12 21:20:55

8
var str = ""; 
for(var k in obj) 
    if (obj.hasOwnProperty(k)) //omit this test if you want to see built-in properties 
     str += k + " = " + obj[k] + "\n"; 
alert(str); 
+0

console.log()如何做到這一點? :) – Christian 2011-03-18 20:28:04

+0

使用Firefox或Chrome。獲取Firefox的Firebug。一個必須有 – moe 2011-03-18 20:29:00

+0

我正在諷刺。 OP特意要求JS代碼,除非你建議他鑽研firebug/fox/chrome,否則我不知道他會在哪裏找到答案。 – Christian 2011-03-18 20:46:28

180

alert(JSON.stringify(object))如何使用現代瀏覽器?

TypeError: Converting circular structure to JSON情況下,這裏有更多的選擇:How to serialize DOM node to JSON even if there are circular references?

的文檔:JSON.stringify()上格式化或美化的輸出提供信息。

+0

+1好的建議。我會添加他可以使用jsonview(http://jsonviewer.stack.hu/)很好地看到它。 – Christian 2011-03-18 21:27:46

+2

如果你想讓它格式化得很好,你可以調用:'alert(JSON.stringify(object,null,4)',其中'4'是用於縮進的空格的數量。 – 2014-08-05 13:02:43

+0

這給了我'undefined'as輸出我試圖調試業障測試 – 2015-03-27 19:41:07

34

使用console.dir(object)和Firebug的插件

+4

這給了我最完整和有用的信息後,我感謝 – 2014-03-04 00:42:09

+1

我沒有意識到'console.dir'功能。我無法弄清楚爲什麼我不能在Firebug中查看完整的對象,現在已經爲我排序了,謝謝! – 2016-07-01 12:04:10

+0

除了顯示方便之外,我還需要知道是否還有其他優點比'console.log'更好,請 – user10089632 2017-08-23 16:45:41

13

使用控制檯:

console.log(object); 
+0

那些顯示的值會動態變化,這對於調試目的可能會產生誤導 – user10089632 2017-08-23 14:55:18

+0

在Chrome中使用您的控制檯首選項並單擊preser ve日誌。現在,即使您的腳本將您重定向到另一頁,您的控制檯也不會被清除。 – 2017-08-23 16:07:30

+0

看來,這個問題是Firefox相關的,因爲在Chrome中,除非你是console.log(),顯示的值仍然是。您可能會建議遷移到Chrome,但有時您正在測試瀏覽器功能的可用性。無論如何感謝提示可能有用。 – user10089632 2017-08-23 16:27:18

15

有幾個方法:

1. typeof tells you which one of the 6 javascript types is the object. 
2. instanceof tells you if the object is an instance of another object. 
3. List properties with for(var k in obj) 
4. Object.getOwnPropertyNames(anObjectToInspect) 
5. Object.getPrototypeOf(anObject) 
6. anObject.hasOwnProperty(aProperty) 

在控制檯下,有時.constructor也許.prototype有用:

console.log(anObject.constructor); 
console.log(anObject.prototype) ; 
4

這是基督徒傑出答案的譁衆取寵。我只是讓它更具可讀性:

/** 
* objectInspector digs through a Javascript object 
* to display all its properties 
* 
* @param object - a Javascript object to inspect 
* @param result - a string of properties with datatypes 
* 
* @return result - the concatenated description of all object properties 
*/ 
function objectInspector(object, result) { 
    if (typeof object != "object") 
     return "Invalid object"; 
    if (typeof result == "undefined") 
     result = ''; 

    if (result.length > 50) 
     return "[RECURSION TOO DEEP. ABORTING.]"; 

    var rows = []; 
    for (var property in object) { 
     var datatype = typeof object[property]; 

     var tempDescription = result+'"'+property+'"'; 
     tempDescription += ' ('+datatype+') => '; 
     if (datatype == "object") 
      tempDescription += 'object: '+objectInspector(object[property],result+' '); 
     else 
      tempDescription += object[property]; 

     rows.push(tempDescription); 
    }//Close for 

    return rows.join(result+"\n"); 
}//End objectInspector 
4

這是我的對象檢查器,它更具可讀性。因爲代碼需要長期在這裏寫下你可以像這樣在http://etto-aa-js.googlecode.com/svn/trunk/inspector.js

使用下載:

document.write(inspect(object)); 
+2

通常情況下(這絕對是官方指導 - 不要發佈鏈接),OTOH,對於這樣的版本,它可以是一種很好,因爲你知道你總是會看着最新的代碼,而不是一些陳舊的東西,這是多年前發佈,可能不會再工作了...... - 另外,這塊巨大的代碼將看起來非常糟糕粘貼到文本牆上,所以回答... - ** Off topic:**如果在SO上有一個方法可以使用代碼的「spoiler」標籤,並且能夠鏈接到外部源並自動更新(如果可能), – BrainSlugs83 2015-06-25 20:14:54