10個很少使用的JavaScript?Console方法分享
你一定聽說過 console.log() ,而且可能一直在使用它。它非常流行,在集成開發(fā)環(huán)境中鍵入時,Visual Studio Intellicode 等工具通常會在其他控制臺方法之前推薦使用它。
在本文中,我們將探討一些最有用的控制臺方法,以及它們在數(shù)據(jù)可視化、調(diào)試等方面的用途。
1. table()
當(dāng)你需要在代碼中以表格形式(如對象數(shù)組)顯示一組對象時, console.table() 方法就會派上用場。以汽車列表為例:
const cars = [
{
color: 'red',
age: 4,
maxSpeed: 120,
},
{
color: 'blue',
age: 2,
maxSpeed: 100,
},
{
color: 'yellow',
age: 3,
maxSpeed: 160,
},
];如何在控制臺中檢查它們? console.log() 是一種典型的方法:
console.log(cars);
在 Chrome 瀏覽器開發(fā)者控制臺中,我們可以檢查我們記錄的對象的各種屬性,層次不限。

我們可以在 Node.js 終端中查看屬性,還可以獲得色彩:

這是一種可以接受的方法,但 console.table() 方法提供了一種更優(yōu)雅的替代方法:
console.table(cars);
console.table() 在 Chrome 瀏覽器控制臺中:

console.table() in Node.js Node.js 中的

顧名思義,它以易于理解的表格形式呈現(xiàn)數(shù)據(jù),就像電子表格一樣。它也適用于數(shù)組陣列。
const arr = [ [1, 3, 5], [2, 4, 6], [10, 20, 30], ]; console.table(arr);

2. assert()
console.assert() 非常適合調(diào)試目的,它接收斷言,并在斷言為 false 時向控制臺寫入錯誤信息。但如果是 true ,則不會發(fā)生任何事情:
const num = 13; console.assert(num > 10, 'Number must be greater than 10'); console.assert(num > 20, 'Number must be greater than 20');
第一個斷言通過是因為 num 大于 10 ,所以控制臺只顯示第二個斷言:

3. trace()
console.trace() 可以幫助您在調(diào)用它的位置輸出當(dāng)前堆棧跟蹤。例如
function a() {
b();
}
function b() {
c();
}
function c() {
console.trace();
}
a();
4. error()
error() 可能是第二種最常用的 Console 方法。在 Chrome 瀏覽器控制臺中,它會以獨特的紅色顯示錯誤信息。
console.error('This is an error message.');
console.log('This is a log message.');
不過,在 Node.js 中不會有這種顏色分離:

不過,信息在內(nèi)部被寫入不同的位置。 console.error() 寫入 stderr 流,而 console.log() 寫入 stdout 流。你可以使用 process.stderr 和 process.stdout 訪問這些流。這對于將錯誤信息和信息重定向到不同的文件非常有用,就像我們在下面的代碼示例中所做的那樣。
const fs = require('fs');
const errorFs = fs.createWriteStream('./error-log.txt');
process.stderr.write = errorFs.write.bind(errorFs);
const infoFs = fs.createWriteStream('./info-log.txt');
process.stdout.write = infoFs.write.bind(infoFs);
console.error('This is an error message.');
console.log('This is a log message.');運行此代碼時,傳遞給 error() 和 log() 的信息將輸出到相應(yīng)的文件,而不是控制臺。
5. warn()
console.warn() 在 Chrome 瀏覽器控制臺中輸出黃色信息,表示警告。
console.warn('This is a warning message');
在 Node.js 中,信息會像 console.error() 一樣寫入 stderr 流。
6. count() 和 countReset()
console.count() 記錄當(dāng)前調(diào)用 count() 的執(zhí)行次數(shù)。這是另一個有用的調(diào)試工具。
function shout(message) {
console.count();
return message.toUpperCase() + '!!!';
}
shout('hey');
shout('hi');
shout('hello');
由于我們沒有指定標(biāo)簽,因此顯示的標(biāo)簽是 default 。我們可以通過為 count() 傳遞一個字符串參數(shù)來做到這一點
function shout(message) {
console.count(message);
return message.toUpperCase() + '!!!';
}
shout('hey');
shout('hi');
shout('hello');
shout('hi');
shout('hi');
shout('hello');
現(xiàn)在,每條信息都有不同的計數(shù)。countReset() 方法將標(biāo)簽的計數(shù)設(shè)回零。
function shout(message) {
console.count(message);
return message.toUpperCase() + '!!!';
}
shout('hi');
shout('hello');
shout('hi');
shout('hi');
shout('hello');
console.countReset('hi');
shout('hi');
7. time(), timeEnd(), and timeLog()
我們可以同時使用這些方法來測量程序中某一特定操作所需的時間。
const arr = [...Array(10)];
const doubles1 = [];
console.time('for of');
let i = 0;
for (; i < 1000; i++) {
for (const item of arr);
}
console.timeLog('for of');
for (; i < 1000000; i++) {
for (const item of arr);
}
console.timeEnd('for of');
console.time('forEach');
i = 0;
for (; i < 1000; i++) {
arr.forEach(() => {});
}
console.timeLog('forEach');
for (; i < 1000000; i++) {
arr.forEach(() => {});
}
console.timeEnd('forEach');
在此,我們將對 for of 和 forEach 循環(huán)進(jìn)行性能比較。 time() 啟動定時器,執(zhí)行向其傳遞的標(biāo)簽所指定的操作。 timeLog() 在不停止計時器的情況下記錄當(dāng)前持續(xù)時間,我們用它來顯示迭代一千次后的時間。 timeEnd() 記錄當(dāng)前持續(xù)時間并停止計時器。我們在一百萬次迭代后調(diào)用它。
看起來 forEach() 比 for of 快。
8. clear()
console.clear() 通過清除日志來清除控制臺中的雜亂信息。
console.log('A log message.');
console.clear();
9. group(), groupCollapsed(), and groupEnd()
console.group() 為其后的控制臺信息添加一級縮進(jìn)。 console.groupEnd() 會將縮進(jìn)程度重置為調(diào)用前面的 console.group() 之前的縮進(jìn)程度。
console.log('This is the outer level');
console.group();
console.log('Level 2');
console.group();
console.log('Level 3');
console.warn('More of level 3');
console.groupEnd();
console.log('Back to level 2');
console.groupEnd();
console.log('Back to the outer level');
console.groupCollapsed() 創(chuàng)建了一個類似 console.group() 的組,但該組是折疊的,直到用戶使用旁邊的 "披露 "按鈕將其展開。
console.log('This is the outer level');
console.group();
console.log('Level 2');
console.groupCollapsed();
console.log('Level 3 ');
console.warn('More of level 3');
console.groupEnd();
console.log('Back to level 2');
console.groupEnd();
console.log('Back to the outer level');
10. dir()
console.log() 將 HTMLElement 記錄為 HTML,我們可以在控制臺中瀏覽:

但是, console.dir() 會將其記錄為一個對象,并顯示一個交互式屬性列表:

總結(jié)
正如你在本文中所看到的,除了 console.log() 之外,還有許多控制臺方法。其中一些只是在控制臺 UI 中用顏色和更好的可視化來點綴,而另一些則可以作為調(diào)試和性能測試的強大工具。
到此這篇關(guān)于10個很少使用的JavaScript Console方法分享的文章就介紹到這了,更多相關(guān)JavaScript Console內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
javascript實現(xiàn)阻止iOS APP中的鏈接打開Safari瀏覽器
這篇文章主要介紹了javascript實現(xiàn)阻止iOS APP中的鏈接打開Safari瀏覽器,這個IOS APP一般是Web APP,否則沒法使用本文的代碼,需要的朋友可以參考下2014-06-06
JavaScript實現(xiàn)清除首尾空白字符再進(jìn)行空白匹配
這篇文章主要為大家詳細(xì)介紹了JavaScript實現(xiàn)清除首尾空白字符再進(jìn)行空白匹配的相關(guān)方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解下2025-12-12
html的DOM中document對象images集合用法實例
這篇文章主要介紹了html的DOM中document對象images集合用法,實例分析了images集合的語法與使用技巧,需要的朋友可以參考下2015-01-01
利用函數(shù)的惰性載入提高javascript代碼執(zhí)行效率
在 addEvent 函數(shù)每次調(diào)用的時候都要走一遍,如果瀏覽器支持其中的一種方法,那么他就會一直支持了,就沒有必要再進(jìn)行其他分支的檢測了2014-05-05
JavaScript中5種調(diào)用函數(shù)的方法
這篇文章主要介紹了JavaScript中5種調(diào)用函數(shù)的方法,本文詳細(xì)的介紹了Javascript中各種函數(shù)調(diào)用的方法及其原理,對于理解JavaScript的函數(shù)有很大的幫助,需要的朋友可以參考下2015-03-03

