console.log()是web开发人员工作中的好朋友。但是你知道控制台包含的不仅仅是console.log()吗?这篇文章要讨论的就是JavaScript中的控制台。; l A5 m/ r% e$ z t* `! ?6 i/ U' V 简介9 Z1 D2 j+ f2 r7 h6 b# I( s4 e
Web控制台是一个工具,主要用于记录与网页相关的信息,例如:网络请求、JavaScript安全错误、警告、CSS等。它使我们能够通过在网页内容中执行JavaScript表达式来与网页交互。1 h1 k1 g% E5 a i
控制台的类型 y+ z5 T' p. r; m. X/ y
console.log()
console.error()
console.warn()
console.clear()
console.time()
console.table()
console.count()
console.group() $ B8 r4 j/ P; j) v
1. console.log() : _, O# M: s# z V7 U+ x) d* ^主要用于将输出信息打印到控制台。log()中可以放入任何类型,无论是强类型、数组、对象、还是布尔值等。0 V [. q# L) s+ ?
) Y! _% f; w; T+ }+ Y t5. console.time()和console.timeEnd()6 O% o9 r' x* L1 C' g
无论何时我们想知道一段代码或一个函数所需要花费的时间,都可以使用JavaScript控制台对象提供的time()和timeEnd()方法。关键是要有一个必须相同的标签,而里面的代码可以是任何东西(函数、对象、甚至直接console.log()都可以)。 : L) H ?; s5 r6 k s
// 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')
// 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')