console.log()是web开发人员工作中的好朋友。但是你知道控制台包含的不仅仅是console.log()吗?这篇文章要讨论的就是JavaScript中的控制台。/ b+ J/ V1 v# q* m* x 简介1 p; S9 f. b) P: {' Y, ?; m( I4 T& p
Web控制台是一个工具,主要用于记录与网页相关的信息,例如:网络请求、JavaScript安全错误、警告、CSS等。它使我们能够通过在网页内容中执行JavaScript表达式来与网页交互。7 W. ]2 b$ X5 _. l
控制台的类型 + L) d- G" ]6 a
console.log()
console.error()
console.warn()
console.clear()
console.time()
console.table()
console.count()
console.group()& y) H+ L# c- {: f8 {' ^: t
1. console.log() * t6 A. H" T+ R2 E0 i- ?主要用于将输出信息打印到控制台。log()中可以放入任何类型,无论是强类型、数组、对象、还是布尔值等。6 J" [+ u1 y9 E6 \% y+ t8 M
// console.warn() method
console.warn('This is a sample Warning')
& {. ~" B. g. T/ V& I, U4 l2 y3 W 4. console.clear() * p( h/ z, M: V6 ^ k& k% c; m4 Y用于清除控制台信息。清除控制台时,如果是基于Chromium的浏览器,将打印一个简单的叠加文本,如下面的截图所示“Console was cleared”,而在Firefox中,则不会返回任何消息。 7 |3 m7 U, z f. n& m
// console.time() and console.timeEnd() method
// console.time() method
console.time("Let's see time console")
let time1 = function(){
console.log('time1 function is running')
}
let time2 = function(){
console.log('time2 function is running')
}
time1()
time2()
// console.timeEnd() method
console.timeEnd('Time Taken')
6 U" N; }; x9 W8 `0 e% D0 ~" ] 6. console.table() , C& i. v1 ?9 g3 w% b! t这个方法允许我们在控制台中生成表格。输入数据必须是数组或显示为表格的对象。 ! C) {, y# M5 a, X
// console.group() and console.groupEnd() method
// console.group() method
console.group('This is a sample group')
console.log('This is a sample group log')
console.warn('This is a sample group warning')
console.error('This is a sample group error')
// console.groupEnd() method
console.groupEnd('This is a sample group')
console.log('This is a new section')