console.log( ) | info( ) | debug( ) | warn( ) | error( )
複製代碼
console.log("console log")
console.info("console info")
console.debug("console debug")
console.warn("console warn")
console.error("console error")
複製代碼
這些控制檯將根據提供給它們的事件類型,直接以適當的顏色打印原始字符串javascript
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script> console.log("console log") console.info("console info") console.debug("console debug") console.warn("console warn") console.error("console error") </script>
<script> console.log("%cText color is green and increased font size", "color: green; font-size: 2rem;") </script>
<script> console.log("Multiple styles: %cred %corange", "color: red", "color: orange", "Additional unformatted message"); </script>
<script> let info1 = [["Suprabha"], ["Frontend Dev"], ["Javascript"]] console.table(info1) </script>
<script> console.group() console.log("Test 1st message") console.group("info") console.log("Suprabha") console.log("Frontend Engineer") console.groupEnd() console.groupEnd() </script>
<script> let info2 = { "name": "Suprabha", "designation": "Frontend Engineer", "social": "@suprabhasupi" } console.dir(info2) </script>
<!-- console.dir -->
<button>console.log打印觸發對象</button>
<button>console.dir打印觸發對象</button>
<script> console.log(document.body, 'bodyHtml'); console.dir(document.body); let oButton = document.getElementsByTagName('button'); oButton[0].onclick = function(event){ console.log(event.target, 'button1'); } oButton[1].onclick = function(event){ console.dir(event.target, 'button2'); } </script>
<script> console.assert(false, "Log me!") </script>
<script> let name = "supi" let msg = "Its not a number" console.assert(typeof msg === "number", {name: name, msg: msg}) </script>
<script> console.count("Hey") console.count("Hey") console.count("Hey") console.count("Hey") </script>
<script> for (let i = 0; i < 5; i++) { console.count() } </script>
<script> console.time("Time") let l = 0; for (let i = 0; i < 5; i++) { l += i } console.log("total", l) console.timeEnd("Time") </script>
</body>
</html>
複製代碼
效果以下html
能夠使用% c 指令將 CSS 樣式應用於控制檯輸出java
console.log("%cText color is green and increased font size", "color: green; font-size: 2rem;")
複製代碼
咱們能夠屢次添加% cjson
console.log("Multiple styles: %cred %corange", "color: red", "color: orange", "Additional unformatted message");
複製代碼
Table ()容許咱們在控制檯中生成一個表。輸入必須是一個數組或一個對象,該對象將以表的形式顯示數組
let info = [["Suprabha"], ["Frontend Dev"], ["Javascript"]]
console.table(info)
複製代碼
要組織控制檯,讓咱們使用 console.group () & console.groupEnd ()markdown
使用控制檯組,將控制檯日誌分組在一塊兒,而每一個分組在層次結構中建立另外一個級別。調用 groupEnd 減小了一個dom
console.group()
console.log("Test 1st message")
console.group("info")
console.log("Suprabha")
console.log("Frontend Engineer")
console.groupEnd()
console.groupEnd()
複製代碼
打印指定對象的 JSON 表示形式函數
let info = {
"name": "Suprabha",
"designation": "Frontend Engineer",
"social": "@suprabhasupi"
}
console.dir(info)
複製代碼
直接打印json可能不明顯,用來打印dom對象就明顯對比性能
<button>console.log打印觸發對象</button>
<button>console.dir打印觸發對象</button>
<script> console.log(document.body, 'bodyHtml'); console.dir(document.body); let oButton = document.getElementsByTagName('button'); oButton[0].onclick = function(event){ console.log(event.target, 'button1'); } oButton[1].onclick = function(event){ console.dir(event.target, 'button2'); } </script>
複製代碼
若是第一個參數爲 false,則記錄消息並將跟蹤堆棧到控制檯 它只會打印錯誤的參數,若是第一個參數是真的,它什麼也不會作測試
console.assert(false, "Log me!")
複製代碼
let name = "supi"
let msg = "Its not a number"
console.assert(typeof msg === "number", {name: name, msg: msg})
複製代碼
這個函數記錄調用 count ()的次數。這個函數接受一個可選的參數標籤 若是提供了 label,此函數將記錄使用該特定標籤調用 count ()的次數
console.count("Hey")
console.count("Hey")
console.count("Hey")
console.count("Hey")
複製代碼
若是省略標籤,函數將記錄在這一行中調用 count ()的次數
for (let i = 0; i < 5; i++) {
console.count()
}
複製代碼
檢查代碼在執行時的性能,Time ()是一種更好的方法來跟蹤 JavaScript 執行所花費的微時間
console.time("Time")
let l = 0;
for (let i = 0; i < 5; i++) {
l += i
}
console.log("total", l)
console.timeEnd(![image.png](/im![image.png](/img/bVcR26L) 複製代碼