Node.js console控制臺簡單用法分析
本文實(shí)例講述了Node.js console控制臺簡單用法。分享給大家供大家參考,具體如下:
在Node.js官方文檔 https://nodejs.org/api/console.html 中描述了控制臺的所有API方法。
Class: Console
- new Console(stdout[, stderr])
- console.assert(value[, message][, ...])
- console.dir(obj[, options])
- console.error([data][, ...])
- console.info([data][, ...])
- console.log([data][, ...])
- console.time(label)
- console.timeEnd(label)
- console.trace(message[, ...])
- console.warn([data][, ...])
當(dāng)輸出目標(biāo)是終端或文件,控制臺函數(shù)都是同步的(以防過早退出丟失信息);當(dāng)目標(biāo)是管道的時候,它是異步的(以防長時間的阻塞)。
node script.js 2> error.log | tee info.log
console.log([data][, …])
向標(biāo)準(zhǔn)輸出另起一行打印
(1)此函數(shù)與 printf() 類似,可以帶多個參數(shù)。%d 為輸出整數(shù),%s 為輸出字符串,%j 為輸出 JSON 的字符串格式。
var name = 'chy'; var age = 24; var jsonInfo = {name:'chy',age:'24',location:'FuZhou'}; console.log('%j => name=%s,age=%d', jsonInfo, name, age); // 輸出 {"name":"chy","age":"24","location":"FuZhou"} => name=chy,age=24
(2)如果沒有匹配到輸出格式的話,會默認(rèn)使用空格最為分隔符,依次打印多余的字符串。
console.log('log', 'arg1', 'arg2', 3); //輸出 log arg1 arg2 3
(3)在JS中對象默認(rèn)就是JSON對象。因此只能用 ‘%j' 或 默認(rèn)的方式 打印。
var person = new Object(); person.name = 'chy'; person.age = 24; person.location = 'FuZhou'; console.log(person); // 輸出 { name:'chy' ,age:24, location:'FuZhou' } console.log('%s %j', person, person); // 輸出 [object Object] {"name":"chy","age":"24","location":"FuZhou"}
console.dir(obj[, options])
對 obj 使用 util.inspect
并將結(jié)果字符串向標(biāo)準(zhǔn)輸出打印。
console.time(label)
標(biāo)記一個時間。
console.timeEnd(label)
結(jié)束一個 同樣標(biāo)簽 的定時器,記錄輸出。浮點(diǎn)類型,單位毫秒,保留3位小數(shù),即精確到微妙。
console.time('cost time'); console.timeEnd('costTime'); //輸出 <node:4500> Warning: No such label 'costTime' for console.timeEnd() console.timeEnd('cost time'); //輸出 cost time: 858.063ms
console.trace(message, […])
打印格式化的信息和當(dāng)前位置的堆棧信息到標(biāo)準(zhǔn)錯誤輸出,以上輸出緊跟在 ‘Trace: message' 之后另起一行后面。
console.assert(value[, message][, …])
類似 assert.ok(value[, message])
。用于判斷變量是否和預(yù)期的是一樣的。
console.assert(true, 'does nothing'); //輸出 undefined console.assert(false, 'Whoops %s', 'didn\'t work'); //輸出 AssertionError: Whoops didn't work assert.ok(true, 'does nothing'); //輸出 undefined assert.ok(false, 'Whoops didn\'t work'); //輸出 AssertionError: Whoops didn't work
希望本文所述對大家nodejs程序設(shè)計(jì)有所幫助。
相關(guān)文章
Node.js學(xué)習(xí)之查詢字符串解析querystring詳解
這篇文章主要給大家介紹了關(guān)于Node.js查詢字符串解析querystring的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用node.js具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。2017-09-09一文教你學(xué)會Nodejs中puppeteer的簡單使用
Puppeteer是一個控制headless Chrome的Node.js API ,是一個 Node.js庫,在瀏覽器中手動完成的大多數(shù)事情都可以通過使用 Puppeteer完成,本文主要介紹了Puppeteer的簡單使用,希望對大家有所幫助2024-01-01基于Node.js + WebSocket打造即時聊天程序嗨聊
這篇文章主要介紹了基于Node.js + WebSocket打造即時聊天程序,有興趣的可以了解一下。2016-11-11Nodejs中fs文件系統(tǒng)模塊的路徑動態(tài)拼接的問題和解決方案
在使用fs模塊操作文件時,如果提供的操作路徑是以./或../開頭的相對路徑時,很容易出現(xiàn)路徑動態(tài)拼接錯誤的問題,所以本文給大家介紹了Nodejs中fs文件系統(tǒng)模塊的路徑動態(tài)拼接的問題和解決方案,需要的朋友可以參考下2024-03-03node.js express安裝及示例網(wǎng)站搭建方法(分享)
下面小編就為大家?guī)硪黄猲ode.js express安裝及示例網(wǎng)站搭建方法(分享)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-08-08