当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。