• Node.js Video Tutorials

NodeJS - console.debug() 方法



Node.js console.debug() 函数用于将信息打印到stdout(标准输出)的新行。它的作用与 Node.js 的console.log() 方法相同。这对于故障排除或理解代码特定部分的工作方式非常有用。现在让我们来看一下console.debug() 方法的语法。

Node.js 的console.debug()Console类的内置方法。

语法

以下是Node.js console.debug()方法的语法:

console.debug(data, args);

参数

此方法接受两个参数。具体描述如下。

  • data − 此参数指定要打印到屏幕上的信息。

  • args − 这是一个可选参数,其中args作为替换值传递到传递给data的信息中。这些args可以通过格式说明符访问。

返回值

此方法不会返回任何值;相反,它会将格式化后的信息打印到stdout的新行,类似于console.log()方法。

示例

Node.js console.debug()方法的工作方式类似于Node.js console.log()方法。此方法接受一个参数 (data)。

在此示例中,我们仅使用data参数调用console.debug()方法。

const console = require('console');
console.debug('Tutorialspoint');
console.debug('Simply Easy Learning at your fingertips...');

输出

正如我们在输出中看到的,我们作为data参数传递的消息被打印到stdout的新行。类似于node.jsconsole.log()方法。

Tutorialspoint
Simply Easy Learning at your fingertips...

示例

node.jsconsole.debug()方法将接受一个可选参数 (args)。

在此示例中,我们使用两个参数dataargs调用console.debug()方法。

const console = require('console');
console.debug('There are %d pancakes in the refrigerator, 4);
console.debug('%s is having a %d pack body', "Nik", 6);

输出

home/cg/root/63a00e1fdca8a/main.js:3
console.debug('There are %d pancakes in the refrigerator, 4);
   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   
SyntaxError: Invalid or unexpected token
   at new Script (vm.js:74:7)
   at createScript (vm.js:246:10)
   at Object.runInThisContext (vm.js:298:10)
   at Module._compile (internal/modules/cjs/loader.js:670:28)
   at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
   at Module.load (internal/modules/cjs/loader.js:612:32)
   at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
   at Function.Module._load (internal/modules/cjs/loader.js:543:3)
   at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
   at startup (internal/bootstrap/node.js:238:19)

注意 − 为获得准确的结果,最好在本地执行上述代码。

正如我们在输出中看到的,我们作为data参数传递的消息,以及在args参数中传递的一些替换值,被打印到stdout的新行。

There are 4 pancakes in the refrigerator
Nik is having a 6 pack body
nodejs_console_module.htm
广告
© . All rights reserved.