FireBug 是一個非常實用的JavaScript以及DOM查看調試工具,是 Firefox 的一個外掛程式。使用 FireBug 調試 AJAX 應用非常方便,終於可以告別 alert 時代了!
Console Logging 函數
FireBug 為所有 Web 頁面提供了一個 console 對象。這個對象有以下函數:
Logging 基礎
console.log(”message”
[,objects]) - 將一個字串列印到控制台。字串可以包含任何“String
Formatting”小節描述的模式。字串後面的對象應該用來取代之前字串中的模式。(譯者註:大家用過C裡面 printf
吧,效果基本是一樣的。)
Logging 等級
通常根據不同的等級來區分Logging的嚴重程度是很有協助的。FireBug 提供了4個等級。為了達到視覺分離的效果,這些函數與 log 不同的地方就是它們在被調用的時候會自動包含一個指向程式碼數的連結。
console.debug(”message” [,objects]) - 記錄一個 debug 訊息。
console.info(”message” [,objects]) - 記錄一個資訊.
console.warn(”message” [,objects]) - 記錄一個警告.
console.error(”message” [,objects]) - 記錄一個錯誤.
斷言
斷言是一條確保代碼規則的非常好的途徑。console 對象包含了一系列各種類型的斷言函數,並且允許你編寫自己的斷言函數。
console.assert(a, “message” [,objects]) - Asserts that an a is true.
console.assertEquals(a, b, “message” [,objects]) - Asserts that a is equal to b.
console.assertNotEquals(a, b, “message” [,objects]) - Asserts that a is not equal to b.
console.assertGreater(a, b, “message” [,objects]) - Asserts that a is greater than b.
console.assertNotGreater(a, b, “message” [,objects]) - Asserts that a is not greater than b.
console.assertLess(a, b, “message” [,objects]) - Asserts that a is less than b.
console.assertNotLess(a, b, “message” [,objects]) - Asserts that a is not less than b.
console.assertContains(a, b, “message” [,objects]) - Asserts that a is in the array b.
console.assertNotContains(a, b, “message” [,objects]) - Asserts that a is not in the array b.
console.assertTrue(a, “message” [,objects]) - Asserts that a is equal to true.
console.assertFalse(a, “message” [,objects]) - Asserts that a is equal to false.
console.assertNull(a, “message” [,objects]) - Asserts that a is equal to null.
console.assertNotNull(a, “message” [,objects]) - Asserts that a is not equal to null.
console.assertUndefined(a, “message” [,objects]) - Asserts that a is equal to #ff0000.
console.assertNotUndefined(a, “message” [,objects]) - Asserts that a is not equal to undefined.
console.assertInstanceOf(a, b, “message” [,objects]) - Asserts that a is an instance of type b.
console.assertNotInstanceOf(a, b, “message” [,objects]) - Asserts that a is not an instance of type b.
console.assertTypeOf(a, b, “message” [,objects]) - Asserts that the type of a is equal to the string b.
console.assertNotTypeOf(a, b, “message” [,objects]) - Asserts that the type of a is not equal to the string b.
測量(Measurement)
下面的一些函數可以讓你方便的測量你的一些代碼。
console.trace() - 記錄執行點的堆棧資訊。
console.time(”name”) - 根據 name 建立一個唯一的計時器。
console.timeEnd(”name”) - 根據 name 停止計時器,並且記錄消耗的時間,以毫秒為單位。
console.count(”name”) - 記錄該行代碼執行的次數。
字串格式化
所有 console 的 logging 函數都可以通過以下模式格式化字串:
%s - 將對象格式化為字串。
%d, %i, %l, %f - 將對象格式化為數字。
%o - 將對象格式化成一個指向 inspector 的超連結。
%1.o, %2.0, etc.. - 將對象格式化成包含自己屬性的可互動的表格。
%.o - 將對象格式化成具有自身屬性的一個數組。
%x - 將對象格式化成一個可互動的 XML 樹形結構。
%1.x, %2.x, etc.. - 將對象格式化成一個可互動的 XML 數型結構,並且展開 n 層節點。
如果你需要一個真實的 % 符號,你可以通過一個轉移符號就像這樣 “\%”。
命令列函數
內建的命令列函數可以通過以下命令列使用:
$(”id”) - document.getElementById() 的簡寫。(譯者註:跟 prototype.js 學來的吧?)
$$(”css”) - 返回一個符合 CSS 選取器的元素數組。
$x(”xpath”) - 返回一個符合 XPath 選取器的元素數組。
$0 - 返回最近被檢查(inspected)的對象。
$1 - 返回最近被檢查(inspected)的下一個對象。
$n(5) - 返回最近被檢查的第n個對象。
inspect(object) - 將對象顯示在 Inspector 中。
dir(object) - 返回一個對象的屬性名稱數組。(譯者註:跟 Python 學的?)
clear() - 清除控制台資訊。
Javascript的調試利器:Firebug使用詳解:
http://blog.csdn.net/tianxiaode/archive/2007/09/02/1769152.aspx
初識Firebug:
http://www.ooso.net/index.php/archives/294