Window
、
WorkerGlobalScope
以及在workers中的特殊定義。
下面介紹對象可用的方法以及對應方法的使用示例。javascript
Console.assert()
Console.count()
Console.debug()
Console.log()
Console.dir()
Console.error()
Console._exception()
Console.error()
Console.group()
Console.groupCollapsed()
Console.groupEnd()
Console.info()
Console.log()
Console.profile()
Console.profileEnd()
Console.table()
Console.time()
Console.timeEnd()
Console.trace()
Console.warn()
console對象中較多使用的主要有四個方法console.log()
, console.info()
, console.warn()
, 和console.error()
。他們都有特定的樣式,若是有C開發經驗的同窗會發現它能夠格式化打印字符相似於printf方法。若是你想要爲console打印的字符定義css或者打印圖片的話能夠參考這篇博客 php
var someObject = { str: "Some text", id: 5 }; console.log(someObject);
打印結果相似下面:css
[09:27:13.475] ({str:"Some text", id:5})
你能夠打印多個對象,就像下面同樣:html
var car = "Dodge Charger"; var someObject = {str:"Some text", id:5}; console.info("My first car was a", car, ". The object is: ", someObject);
打印結果相似下面:java
[09:28:22.711] My first car was a Dodge Charger . The object is: ({str:"Some text", id:5})
Gecko 9.0 (Firefox 9.0 / Thunderbird 9.0 / SeaMonkey 2.6) 首次發佈對string substitutions的支持.你能夠在傳遞給console的方法的時候使用下面的字符以期進行參數的替換。web
Substitution string | Description |
%o | 打印javascript對象,能夠是整數、字符串以及JSON數據 |
%d or %i | 打印整數 |
%s | 打印字符串 |
%f | 打印浮點數 |
當要替換的參數類型和預期的打印類型不一樣時,參數會被轉換成預期的打印類型。chrome
for (var i=0; i<5; i++) { console.log("Hello, %s. You've called me %d times.", "Bob", i+1); } console.log("I want to print a number:%d","string")
The output looks like this:api
[13:14:13.481] Hello, Bob. You've called me 1 times. [13:14:13.483] Hello, Bob. You've called me 2 times. [13:14:13.485] Hello, Bob. You've called me 3 times. [13:14:13.487] Hello, Bob. You've called me 4 times. [13:14:13.488] Hello, Bob. You've called me 5 times. [13:14:13.489] I want to print a number:NaN
咱們發現"string"字符串被轉換成數字失敗成轉換成 NaN [en-US]瀏覽器
你可使用"%c"爲打印內容定義樣式:緩存
console.log("%cMy stylish message", "color: red; font-style: italic");
你可使用console.group方法來組織本身的打印內容以期得到更好的顯示方式。
示例:
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.debug("Back to the outer level");
執行結果:
console.time("answer time"); alert("Click to continue"); console.timeEnd("answer time");
打印結果:
打印當前執行位置到console.trace()
的路徑信息.。
foo(); function foo() { function bar() { console.trace(); } bar(); }
The output in the console looks something like this: