控制台
方法
log()
outputs a message to the web console.
The message may be a single string (with optional substitution values), or it may be any one or more JavaScript objects.
console.log(obj1 [, obj2, ..., objN]); console.log(msg [, subst1, ..., substN]);
obj1
...
objN
console.log()
, but it is the value of the object at the moment you open the console.
msg
A JavaScript string containing zero or more substitution strings.
subst1
...
substN
msg
. This gives you additional control over the format of the output.
见
把文本输出到控制台
在文档编制的
console
了解细节。
You might ask yourself what's the difference between
console.dir()
and
console.log()
.
Another useful difference in Chrome exists when sending DOM elements to the console.
预告:
console.log
以像 HTML 树打印元素
console.dir
以像 JSON 树打印元素
具体来说,
console.log
gives special treatment to DOM elements, whereas
console.dir
does not. This is often useful when trying to see the full representation of the DOM JS object.
有更多信息在 Chrome 控制台 API 参考 关于此和其它函数。
不使用
console.log(obj)
,使用
console.log(JSON.parse(JSON.stringify(obj)))
.
This way you are sure you are seeing the value of
obj
at the moment you log it. Otherwise, many browsers provide a live view that constantly updates as values change. This may not be what you want.
| 规范 | 状态 | 注释 |
|---|---|---|
|
控制台 API
在该规范中的 console.log() 定义。 |
实时标准 | 初始定义 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
log
|
Chrome 1 | Edge 12 | Firefox 4 | IE 8 | Opera 10.5 | Safari 3 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 11 | Safari iOS 1 | Samsung Internet Android 1.0 |
| Substitution strings |
Chrome
Yes
|
Edge
12
|
Firefox 9 |
IE
10
|
Opera ? | Safari ? |
WebView Android
Yes
|
Chrome Android
Yes
|
Firefox Android 9 | Opera Android ? | Safari iOS ? |
Samsung Internet Android
Yes
|
完整支持
兼容性未知
见实现注意事项。