标签:
Window、WorkerGlobalScope以及在workers中的特殊定义。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或者打印图片的话可以参考这篇博客
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})
Gecko 9.0 (Firefox 9.0 / Thunderbird 9.0 / SeaMonkey 2.6) 首次发布对string substitutions的支持.你可以在传递给console的方法的时候使用下面的字符以期进行参数的替换。
| Substitution string | Description |
| %o | 打印javascript对象,可以是整数、字符串以及JSON数据 |
| %d or %i | 打印整数 |
| %s | 打印字符串 |
| %f | 打印浮点数 |
当要替换的参数类型和预期的打印类型不同时,参数会被转换成预期的打印类型。
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:
[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:

标签:
原文地址:http://www.cnblogs.com/AllenChou/p/5855833.html