• Node.js Video Tutorials

NodeJS - console.groupEnd() 方法



Node.js 的console.groupEnd() 方法是Console 類的一個內建方法。

Node.js 的 console.groupEnd() 方法將結束由 console.group()console.groupCollapsed() 方法建立的組。

此方法標誌著組的結束。它用於結束先前啟動的控制檯分組;它可以與 console.group() 方法一起使用。

它建立了一個新的縮排訊息組,以便於閱讀和組織輸出。現在讓我們深入瞭解 Node.js 的 console.groupEnd() 方法的語法和用法。

語法

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

console.groupEnd();

引數

此方法不接受任何引數。

返回值

此方法不會返回任何內容,而是結束控制檯上的組。

示例

在這個例子中,我們使用 Node.js 的 console.group() 方法建立一個組,並使用 console.log() 函式編寫一些訊息。然後,我們透過呼叫 Node.js 的 console.groupEnd() 方法結束該組。

console.group("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.log("group is ended");

輸出

從下圖的輸出中可以看到,我們已經結束了該組。

GROUP 1
Knock knock....first message in Group 1
Knock knock....second message in Group 1
Done with the messages, closing the group now
group is ended

示例

在這個例子中,我們使用 console.groupCollapsed() 方法建立一個組,並使用 console.log() 函式編寫一些訊息。然後,我們透過呼叫 console.groupEnd() 方法結束該組。

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

輸出

Collapsed group
Knock knock....first message in Collapsed group
Knock knock....second message in Collapsed group
Done with the messages, closing the group now
group is ended

為了更好地理解,請在瀏覽器的控制檯中執行上面的程式碼。以下是上述程式在瀏覽器控制檯中的輸出。

group_end

由於該組是使用 console.groupCollapsed() 方法建立的,我們需要展開內部訊息。然後,我們使用 console.groupEnd() 方法結束該組。

console_group_end

示例

在這個例子中,我們建立了巢狀組。我們使用 console.group() 方法建立了一個父組,並使用 console.groupCollapsed() 方法建立了巢狀方法。然後,我們按順序結束所有組。

console.group("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.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.log("GROUP 1 ended");

輸出

GROUP 1
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
GROUP 1 ended
nodejs_console_module.htm
廣告