console.log()是web开发人员工作中的好朋友。但是你知道控制台包含的不仅仅是console.log()吗?这篇文章要讨论的就是JavaScript中的控制台。7 j2 f' g2 g9 p, w 简介 ' Z! ^7 I- G! yWeb控制台是一个工具,主要用于记录与网页相关的信息,例如:网络请求、JavaScript安全错误、警告、CSS等。它使我们能够通过在网页内容中执行JavaScript表达式来与网页交互。+ w( b: x {$ ]4 N6 O
控制台的类型3 }+ V. u4 X. G u' |) u% c
console.log()
console.error()
console.warn()
console.clear()
console.time()
console.table()
console.count()
console.group()+ m a/ A' i9 l: J
1. console.log() / V6 g0 K0 B' {: h主要用于将输出信息打印到控制台。log()中可以放入任何类型,无论是强类型、数组、对象、还是布尔值等。 / }/ M4 x" M- J0 p5 W/ R2 o: I
// console.warn() method
console.warn('This is a sample Warning')
8 Q n! ?+ T4 i) K# h 4. console.clear() - }& V6 o R5 z3 w. y& t3 |用于清除控制台信息。清除控制台时,如果是基于Chromium的浏览器,将打印一个简单的叠加文本,如下面的截图所示“Console was cleared”,而在Firefox中,则不会返回任何消息。 + f H) ^/ h% A5 b
// 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')
1 k( @ B( \% p* o0 | 6. console.table() ) X: Z) d f, Z6 j7 k这个方法允许我们在控制台中生成表格。输入数据必须是数组或显示为表格的对象。( s0 ]0 E. F! C0 y L& A$ J* e9 Y
// 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')