當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Node.js console.group()用法及代碼示例


控製台。 group()方法是控製台模塊的內置應用程序編程接口,用於以分組方式打印傳遞的內容。它指示消息組的開始。要結束該組,我們可以使用控製台。 groupEnd()方法。

用法:

console.group([label]);

參數:如上所述,此方法僅接受一個參數,如下所述:

  1. label: 您在控製台上分組的組的標簽。此參數不是必需的,它是可選的,可以在控製台上運行。沒有傳遞標簽參數的group()方法。

參數值:

  1. Type:
  2. Required:可選的

返回值:此方法不會返回任何內容,但會在控製台上用標簽將內容分組(如果提供的話,否則會簡單地不帶標簽)。



以下示例說明了控製台的使用。 Node.js中的group()方法。

範例1:

index.js


// Node.js program to demonstrate the 
// console.group() method 
// This code example demonstrate the
// method without parameter
  
// Accessing console module 
const console = require('console'); 
  
console.log("GeeksforGeeks");
console.log();
  
// Creating  First Group
console.group();
  
// Printing First Statement of Group 
console.log("1st print from the first group");
  
// Printing Second Statement of Group 
console.log("2nd print from the first group");
  
// Ending  the group 
console.groupEnd();
console.log();

使用以下命令運行index.js文件:

node index.js

控製台輸出:

GeeksforGeeks

 1st print from the first group
 2nd print from the first group

範例2:

index.js


// Node.js program to demonstrate the 
// console.group() method 
  
// This code example demonstrate the method
// with parameter and nested groups
  
// Accessing console module 
const console = require('console');
  
console.log("GeeksforGeeks ");
  
console.log("==========================");
console.log();
  
// Creating First group
console.group("This is the starting main group");
console.log();
  
// Printing First statement of Group 
console.log("1st group and not from any nested group");
console.log();
  
// Creating nested group in First group 
console.group("Starting of the 1st Nested group");
  
// Printing 1st line of nested group
console.log("Hello Geeks for Geeks from "
    + "1st Main Nested group");
  
// Printing 2nd line of nested group 
console.log("Computer Science Portal(GeeksforGeeks)"
    + " from 1st Main Nested group");
  
// Ending of the first nested group
console.groupEnd();
  
console.log();
console.log();
  
// Creating second nested group 
console.group("Starting of the 2nd Main Nested group");
  
// Printing 1st line of second nested group
console.log("Hello from 2nd Main Nested group");
  
// Printing 2nd line of second nested group
console.log("Hello from 2nd Main Nested group");
  
// Ending of the second nested group
console.groupEnd();
console.log();
  
// Printing last line of the group
console.log("Now Main group will end");
  
console.log();
  
// Calling groupEnd() method (It is used to 
// end the group for more understanding 
// the group method) 
console.groupEnd();
  
// Printing from outside the Main group
console.log("This the Hello from GeeksforGeeks"
    + " from outside the Main Group");
  
console.log();
console.log("===========================");

控製台輸出:

GeeksforGeeks 
==========================

This is the starting main group
  
  1st group and not from any nested group
  
  Starting of the 1st Nested group
    Hello Geeks for Geeks from 1st Main Nested group
    Computer Science Portal(GeeksforGeeks) 
      from 1st Main Nested group
  
  
  Starting of the 2nd Main Nested group
    Hello from 2nd Main Nested group
    Hello from 2nd Main Nested group
  
  Now Main group will end
  
This the Hello from GeeksforGeeks from 
  outside the Main Group        

===========================

參考: https://nodejs.org/api/console.html#console_console_group_label

相關用法


注:本文由純淨天空篩選整理自aditya32123大神的英文原創作品 Node.js console.group() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。