机械秒表的使用方法_让console.log()不再是你的唯一选项js日志输出6种方法
幾乎所有的javascript開發(fā)者最常使用的日志打印調(diào)試api都是console.log(),其實(shí)還有很多的選項(xiàng)供我們選擇,筆者下面就為大家一一介紹.
一、console.table()
console.table()是我非常建議大家去使用的方法,它可以接受JSON或數(shù)組并以表格格式打印,在對(duì)json對(duì)象和數(shù)組進(jìn)行可視化打印的時(shí)候簡(jiǎn)單易用,結(jié)果直觀。
比如下面的json數(shù)據(jù)對(duì)象使用console.table()打印
console.table({"id":"1",
"key":"value",
"count":2
});
控制臺(tái)的輸出結(jié)果如下:
又比如對(duì)下面代碼中的數(shù)組進(jìn)行打印:
console.table([{
id: "1",
key: "value",
count: 2,
},
{
id: "2",
key: "value2",
count: 22,
},
{
id: "3",
key: "value3",
count: 5,
},
]);
控制臺(tái)的輸出結(jié)果如下:
二、console.error()
console.error()相對(duì)于console.log()更有助于在調(diào)試時(shí)從輸出日志中區(qū)分錯(cuò)誤信息
從上圖中可以看到,它的輸出打印結(jié)果是紅色的。
三、Time(time,timeLog,timeEnd)
console.time()、console.timeLog()、console.timeEnd() 這三個(gè)方法當(dāng)我們對(duì)程序運(yùn)行時(shí)間進(jìn)行計(jì)時(shí)的時(shí)候特別有用。
參考下圖理解這三個(gè)方法
console.time()相當(dāng)于秒表中的開始按鈕
console.timeLog()相當(dāng)于秒表中的按圈計(jì)時(shí)/按點(diǎn)計(jì)時(shí)
console.timeEnd()相當(dāng)于計(jì)時(shí)結(jié)束
// "ForLoop" is label here
for (let i = 0; i < 5; i++) {
console.timeLog('ForLoop');
}
console.timeEnd("ForLoop");
控制臺(tái)打印輸出結(jié)果
四、console.warn()
用黃色字體輸出日志,更直觀的方便的查看警告類日志信息。
五、console.assert()
console.assert(assert_statement,message)用來設(shè)定斷言,如果為false則顯示message消息
if(3!=2){console.error({ msg1: "msg1", msg2: "msg2" });
}
//上面的日志判斷語句,可以簡(jiǎn)寫為下面的斷言
console.assert(3 === 2, { msg1: "msg1", msg2: "msg2" });
另一種可以用來格式化輸出的斷言方式console.assert(assert_statement,message,args)
console.assert(false, "%d nd type for %s ",2,"console.assert() method");六、console.count()
console.count()特別適合用來計(jì)數(shù),可以傳遞參數(shù),可以根據(jù)根據(jù)參數(shù)標(biāo)簽統(tǒng)計(jì)次數(shù)。代碼如下:
for (let i = 0; i < 3; i++) {console.count("label");
console.count();
console.count(i);
}
控制臺(tái)打印輸出的結(jié)果,類似于下面這樣
console.count() console.count("label") console.count(i)default: 1 label: 1 0: 1
default: 2 label: 2 1: 1
default: 3 label: 3 2: 1
console.count()如果不傳遞參數(shù),則使用默認(rèn)的default標(biāo)簽。
console.countReset(標(biāo)簽參數(shù))可以將指定標(biāo)簽的計(jì)數(shù)重置為0
總結(jié)
以上是生活随笔為你收集整理的机械秒表的使用方法_让console.log()不再是你的唯一选项js日志输出6种方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 原生android字体,不用Root,国
- 下一篇: tableview直接滚动至最后一行