• 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.