• Node.js Video Tutorials

NodeJS - console.groupCollapsed() 方法



Node.js 的 console.groupCollapsed() 方法Console 模組的內建方法。

Node.js 的 console.groupCollapsed() 方法將確保建立的組處於摺疊狀態,直到我們呼叫 console.groupEnd() 方法,即它顯示新的組,而沒有額外的縮排。

預設情況下,我們使用 console.group() 建立的組將在控制檯中展開。但是,如果我們使用 console.groupCollapsed() 建立一個組,則該組內的所有訊息都將被摺疊,即它們不會展開。

語法

以下是 Node.js console.groupCollapsed() 方法的語法:

console.groupCollapsed([label]);

引數

  • label - 該引數充當組的標籤。這將是我們在 console 中分組的組的標題。這不是必填引數;我們也可以在不帶引數(標籤)的情況下使用此方法。

返回值

此方法不返回值;它只會摺疊組內的訊息。

以下示例在不同場景中演示了 Node.js 的 console.groupCollapsed() 方法。

示例

Node.js 的 console.groupCollapsed() 方法只會接受一個 label 引數(可選引數)。

注意 - 預設情況下,組將在 console 中展開。只有在我們使用 console.groupCollapsed() 方法時,它才會摺疊。

  • 我們正在使用 console.groupCollapsed() 建立一個組,並傳遞一個引數(label)。此標籤將用作組標題。

  • 然後,我們使用 console.log() 函式在組內寫入一些訊息。

  • 之後,我們使用 Node.js 的 console.groupEnd() 方法結束該組。

console.groupCollapsed("Collapsed Group 1");
console.log("Knock knock....first message in Group 1");
console.log("Knock knock....second message in Group 1")
console.log("Done with the messages, closing the group now");
console.groupEnd();

console.groupCollapsed("Collapsed Group 2");
console.log("Knock knock....first message in Group 2");
console.log("Knock knock....second message in Group 2")
console.log("Done with the messages, closing the group now");
console.groupEnd();

輸出

Collapsed Group 1
Knock knock....first message in Group 1
Knock knock....second message in Group 1
   Done with the messages, closing the group now
Collapsed Group 2
   Knock knock....first message in Group 2
Knock knock....second message in Group 2
   Done with the messages, closing the group now

為了更好地理解,請在瀏覽器的控制檯中執行上述程式碼。如果我們在瀏覽器的控制檯中執行它,則輸出如下。

當我們開啟控制檯時,該組將處於摺疊狀態。要展開,我們需要點選展開按鈕。

expand_button

點選展開按鈕後,我們可以看到組內的訊息。

collapse_gruop

示例

在下面的示例中,我們透過在一個組內呼叫一個組來建立巢狀組,然後我們相應地關閉這些組。

console.groupCollapsed("Parent group");
console.log("Knock knock....first message in Group 1");
console.log("Knock knock....second message in Group 1")
console.log("Done with the messages, closing the group now");
    
console.groupCollapsed("Nested group 1");
console.log("Knock knock....first message in Nested group 1");
console.log("Knock knock....second message in Nested group 1")
console.log("Now we are entering into another group inside nested group");

console.groupCollapsed("inner nested group");
console.log("OOPS! no messages here.");
console.groupEnd();
console.log("inner nested group ended");
console.groupEnd();
console.log("Nested group ended");
console.groupEnd();
console.log("Parent group ended");

輸出

正如我們在下面的輸出中看到的,主組內的所有巢狀組都將被摺疊,因為我們使用 console.groupCollapsed() 方法建立了這些組。

Parent group
Knock knock....first message in Group 1
Knock knock....second message in Group 1
   Done with the messages, closing the group now
   Nested group 1
   Knock knock....first message in Nested group 1
   Knock knock....second message in Nested group 1
   Now we are entering into another group inside nested group
   inner nested group
      OOPS! no messages here.inner nested group ended
   Nested group ended
Parent group ended

為了更好地理解,請在瀏覽器的控制檯中執行上述程式碼。如果我們在瀏覽器的控制檯中執行它,則輸出如下。

當我們開啟控制檯時,我們可以看到“父組”內的所有組都已摺疊。

parent_group

當我們展開“父組”時,它將顯示父組內的訊息。

nested_group1

然後,“巢狀組 1”也被摺疊。要檢視訊息,我們需要展開。

nested_group1_collapse

最終,“內部巢狀組”也被摺疊。我們展開以檢視訊息。

inner_nested_group
nodejs_console_module.htm
廣告

© . All rights reserved.