翻译不完整。 请帮助我们翻译这篇文章!
Console 对象可以接入浏览器控制台(如:Firefox 的 Web Console)。在不同浏览器上它的实现细节可能是不一样的,但这里会介绍一些典型的浏览器会提供的特性。
Console 对象可以从任何全局对象中访问到,如 Window,WorkerGlobalScope 以及控制台属性中的特殊变量。它被定义为 Window.console,而且可直接通过 console 调用。例:
console.log("Failed to open the specified link")
本页面记录了 Console 对象中的 Methods 并给出了几个 Usage 示例。
提示: 由于历史原因,console 接口全部被定义为小写(在 i.e. 上不能使用 Console)。
方法
Console.assert()- 判断第一个参数是否为真,
false的话抛出异常并且在控制台输出相应信息。 Console.clear()- 清空控制台,并输出
Console was cleared。 Console.count()- 以参数为标识记录调用的次数,调用时在控制台打印标识以及调用次数。
Console.countReset()- 重置指定标签的计数器值。
Console.debug()- 在控制台打印一条
"debug"级别的消息。提示: 从 Chromium 58 开始,Chromium 浏览器打印的debug消息仅在日志级别为 “Verbose” 时可见。 Console.dir()- 打印一条以三角形符号开头的语句,可以点击三角展开查看对象的属性。This listing lets you use disclosure triangles to examine the contents of child objects.
Console.dirxml()- 打印 XML/HTML 元素表示的指定对象,否则显示 JavaScript 对象视图。
Console.error()- 打印一条错误信息,使用方法可以参考 string substitution。
Console.exception()error()方法的别称。Console.group()- 创建一个新的内联 group, 后续所有打印内容将会以子层级的形式展示。调用
groupEnd()来闭合组。 Console.groupCollapsed()- 创建一个新的内联 group。使用方法和
group()相同,不同的是,groupCollapsed()方法打印出来的内容默认是折叠的。To move back out a level, callgroupEnd(). Console.groupEnd()- 闭合当前内联 group.
Console.info()- 打印资讯类说明信息,使用方法可以参考 string substitution。
Console.log()- 打印内容的通用方法,使用方法可以参考 string substitution。
Console.profile()- Starts the browser's built-in profiler (for example, the Firefox performance tool). You can specify an optional name for the profile.
Console.profileEnd()- Stops the profiler. You can see the resulting profile in the browser's performance tool (for example, the Firefox performance tool).
Console.table()- 将列表型的数据打印成表格。
Console.time()- 启动一个以入参作为特定名称的计时器,在显示页面中可同时运行的计时器上限为10,000.
Console.timeEnd()- 结束特定的 计时器 并以豪秒打印其从开始到结束所用的时间。
Console.timeLog()- 打印特定 计时器 所运行的时间。
Console.timeStamp()- 添加一个标记到浏览器的 Timeline 或 Waterfall 工具。
Console.trace()- 输出一个 stack trace。
Console.warn()- 打印一个警告信息,可以使用 string substitution 和额外的参数。
用法
输出文本到控制台
console 对象中较多使用的主要有四个方法 console.log(), console.info(), console.warn(), 和console.error()。每一个结果在日志中都有不同的样式,可以使用浏览器控制台的日志筛选功能筛选出感兴趣的日志信息。
有两种途径使用这些方法,你可以简单的传入一组对象,其中的字符串对象会被连接到一起,输出到控制台。或者你可以传入包含零个或多个的替换的字符串,后面跟着被替换的对象列表。
打印单个对象
The simplest way to use the logging methods is to output a single object:
var someObject = { str: "Some text", id: 5 };
console.log(someObject);
打印结果类似下面:
[09:27:13.475] ({str:"Some text", id:5})
打印多个对象
你可以打印多个对象,就像下面一样:
var car = "Dodge Charger";
var someObject = { str: "Some text", id: 5 };
console.info("My first car was a", car, ". The object is:", someObject);
打印结果类似下面:
[09:28:22.711] My first car was a Dodge Charger . The object is: ({str:"Some text", id:5})
使用字符串替换
你可以在传递给 console 的方法的时候使用下面的字符以期进行参数的替换。
| Substitution string | Description |
%o or %O |
打印 JavaScript 对象,可以是整数、字符串或是 JSON 数据。Clicking the object name opens more information about it in the inspector. |
%d or %i |
打印整数。Number formatting is supported, for example console.log("Foo %.2d", 1.1) will output the number as two significant figures with a leading 0: Foo 01 |
%s |
打印字符串。 |
%f |
打印浮点数。Formatting is supported, for example console.log("Foo %.2f", 1.1) will output the number to 2 decimal places: Foo 1.10 |
注意:Chrome 不支持精确格式化。
当要替换的参数类型和预期的打印类型不同时,参数会被转换成预期的打印类型。
for (var i=0; i<5; i++) {
console.log("Hello, %s. You've called me %d times.", "Bob", i+1);
}
输出样例如下所示:
[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.
为控制台定义样式
你可以使用 %c 为打印内容定义样式:
console.log("This is %cMy stylish message", "color: yellow; font-style: italic; background-color: blue;padding: 2px");

Note: Quite a few CSS properties are supported by this styling; you should experiment and see which ones prove useful.
Using groups in the console
You can use nested groups to help organize your output by visually combining related material. To create a new nested block, call console.group(). The console.groupCollapsed() method is similar but creates the new block collapsed, requiring the use of a disclosure button to open it for reading.
groupCollapsed() method is the same as group() at this time.To exit the current group, simply call console.groupEnd(). For example, given this code:
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");
执行结果:

定时器
In order to calculate the duration of a specific operation, Gecko 10 introduced the support of timers in the console object. To start a timer, call the console.time() method, giving it a name as the only parameter. To stop the timer, and to get the elapsed time in milliseconds, just call the console.timeEnd() method, again passing the timer's name as the parameter. Up to 10,000 timers can run simultaneously on a given page.
For example, given this code:
console.time("answer time");
alert("Click to continue");
console.timeLog("answer time");
alert("Do a bunch of other stuff...");
console.timeEnd("answer time");
Will log the time needed by the user to dismiss the alert box, log the time to the console, wait for the user to dismiss the second alert, and then log the ending time to the console:

Notice that the timer's name is displayed both when the timer is started and when it's stopped.
堆栈跟踪
The console object also supports outputting a stack trace; this will show you the call path taken to reach the point at which you call console.trace(). Given code like this:
function foo() {
function bar() {
console.trace();
}
bar();
}
foo();
控制台的输出:

规范
| 规范 | 状态 | 备注 |
|---|---|---|
| Console API | Living Standard | Initial definition. |
浏览器兼容性
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
Console | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 2 | IE
Full support
8
| Opera Full support 10.5 | Safari Full support 3 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 11 | Safari iOS Full support 1 | Samsung Internet Android Full support 1.0 |
assert | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 28 | IE Full support 8 | Opera Full support 11 | Safari Full support 4 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 28 | Opera Android Full support 11 | Safari iOS Full support 3.2 | Samsung Internet Android Full support 1.0 |
clear | Chrome Full support Yes | Edge Full support 12 | Firefox Full support 48 | IE Full support Yes | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Firefox Android Full support 48 | Opera Android ? | Safari iOS ? | Samsung Internet Android Full support Yes |
count | Chrome Full support Yes | Edge Full support 12 | Firefox Full support 30 | IE Full support Yes | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Firefox Android Full support 30 | Opera Android ? | Safari iOS ? | Samsung Internet Android Full support Yes |
countReset | Chrome Full support Yes | Edge Full support ≤79 | Firefox Full support 62 | IE No support No | Opera Full support Yes | Safari ? | WebView Android Full support Yes | Chrome Android Full support Yes | Firefox Android Full support 62 | Opera Android ? | Safari iOS ? | Samsung Internet Android Full support Yes |
debug | Chrome Full support Yes | Edge Full support 12 | Firefox Full support 4 | IE Full support Yes | Opera ? | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Firefox Android Full support 4 | Opera Android ? | Safari iOS ? | Samsung Internet Android Full support Yes |
dir | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 8 | IE Full support 9 | Opera Full support 11 | Safari Full support 4 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 8 | Opera Android Full support 11 | Safari iOS Full support 3.2 | Samsung Internet Android Full support 1.0 |
dirxml | Chrome Full support Yes | Edge Full support 12 | Firefox Full support Yes | IE Full support Yes | Opera Full support Yes | Safari ? | WebView Android Full support Yes | Chrome Android Full support Yes | Firefox Android No support No | Opera Android ? | Safari iOS ? | Samsung Internet Android Full support Yes |
error | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 4 | IE Full support 8 | Opera Full support 10.5 | Safari Full support 3 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 11 | Safari iOS Full support 1 | Samsung Internet Android Full support 1.0 |
exception (an alias for error) | Chrome No support No | Edge No support 13 — 79 | Firefox Full support 28 | IE No support No | Opera No support No | Safari No support No | WebView Android No support No | Chrome Android No support No | Firefox Android Full support 28 | Opera Android ? | Safari iOS No support No | Samsung Internet Android No support No |
group | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 4 | IE Full support 11 | Opera Full support Yes | Safari Full support 4 | WebView Android Full support 37 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android ? | Safari iOS ? | Samsung Internet Android Full support 1.0 |
groupCollapsed | Chrome Full support 6 | Edge Full support 12 | Firefox Full support 52 | IE Full support 11 | Opera ? | Safari Full support 5.1 | WebView Android Full support 37 | Chrome Android Full support 18 | Firefox Android Full support 52 | Opera Android ? | Safari iOS ? | Samsung Internet Android Full support 1.0 |
groupEnd | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 9 | IE Full support 11 | Opera Full support Yes | Safari Full support 4 | WebView Android Full support 37 | Chrome Android Full support 18 | Firefox Android Full support 9 | Opera Android ? | Safari iOS ? | Samsung Internet Android Full support 1.0 |
info | Chrome Full support Yes | Edge Full support 12 | Firefox Full support 4 | IE Full support 8 | Opera Full support Yes | Safari
Full support
Yes
| WebView Android Full support Yes | Chrome Android Full support Yes | Firefox Android Full support 4 | Opera Android ? | Safari iOS ? | Samsung Internet Android Full support Yes |
log | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 4 | IE Full support 8 | Opera Full support 10.5 | Safari Full support 3 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 11 | Safari iOS Full support 1 | Samsung Internet Android Full support 1.0 |
profile | Chrome Full support 53 | Edge Full support 12 | Firefox Full support Yes | IE Full support Yes | Opera ? | Safari ? | WebView Android Full support 53 | Chrome Android Full support 53 | Firefox Android Full support 10 | Opera Android ? | Safari iOS ? | Samsung Internet Android Full support 6.0 |
profileEnd | Chrome Full support Yes | Edge Full support 12 | Firefox Full support Yes | IE Full support Yes | Opera ? | Safari ? | WebView Android Full support Yes | Chrome Android Full support Yes | Firefox Android Full support 10 | Opera Android ? | Safari iOS ? | Samsung Internet Android Full support Yes |
table | Chrome Full support 27 | Edge Full support 13 | Firefox Full support 34 | IE No support No | Opera Full support 11 | Safari Full support 6.1 | WebView Android Full support ≤37 | Chrome Android Full support 27 | Firefox Android Full support 34 | Opera Android Full support 11 | Safari iOS Full support 7 | Samsung Internet Android Full support 1.5 |
time | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 10 | IE Full support 11 | Opera Full support 11 | Safari Full support 4 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 10 | Opera Android Full support 11 | Safari iOS Full support 3.2 | Samsung Internet Android Full support 1.0 |
timeEnd | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 10 | IE Full support 11 | Opera Full support Yes | Safari Full support 4 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 10 | Opera Android ? | Safari iOS ? | Samsung Internet Android Full support 1.0 |
timeLog | Chrome Full support 72 | Edge Full support 79 | Firefox Full support 62 | IE No support No | Opera Full support 60 | Safari
No support
No
| WebView Android Full support 72 | Chrome Android Full support 72 | Firefox Android Full support 62 | Opera Android ? | Safari iOS No support No | Samsung Internet Android No support No |
timestamp | Chrome Full support Yes | Edge Full support 12 | Firefox Full support Yes | IE Full support 11 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Firefox Android Full support 10 | Opera Android ? | Safari iOS ? | Samsung Internet Android Full support Yes |
trace | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 10 | IE Full support 11 | Opera Full support 11 | Safari Full support 4 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 10 | Opera Android Full support 11 | Safari iOS Full support 3.2 | Samsung Internet Android Full support 1.0 |
warn | Chrome Full support Yes | Edge Full support 12 | Firefox Full support 4 | IE Full support 8 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Firefox Android Full support 4 | Opera Android ? | Safari iOS ? | Samsung Internet Android Full support Yes |
| Available in workers | Chrome Full support Yes | Edge Full support 12 | Firefox Full support 38 | IE Full support Yes | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Firefox Android Full support 38 | Opera Android ? | Safari iOS ? | Samsung Internet Android Full support Yes |
Legend
- Full support
- Full support
- No support
- No support
- Compatibility unknown
- Compatibility unknown
- Experimental. Expect behavior to change in the future.
- Experimental. Expect behavior to change in the future.
- Non-standard. Expect poor cross-browser support.
- Non-standard. Expect poor cross-browser support.
- Deprecated. Not for use in new websites.
- Deprecated. Not for use in new websites.
- See implementation notes.
- See implementation notes.
注意
- 在 Firefox 浏览器中,如果页面中自己定义了 console 对象,那么它会覆盖掉浏览器内置的 console 对象,在其它浏览器可能也是。
- Gecko 12.0 之前的版本中,console 对象中的方法仅在控制台打开的情况下才会执行。Gecko 12.0 之后,控制台未打开时要打印的内容会被缓存,打开控制台时就会把它们全部打印出来。
- 值得注意的是,Firefox 内置的
Console对象与 Firebug 插件中的是相互兼容的。
相关文档
- Tools
- Web Console - Firefox 浏览器控制台如何处理 console API 的调用
- Remote debugging - 如何在调试移动设备时查看控制台输出。