知识共享许可协议
本作品采用知识共享署名-非商业性使用 3.0 未本地化版本许可协议进行许可。

Node.js v0.10.18 手册 & 文档


控制台#

稳定度: 4 - 冻结
  • {Object}

  • {Object}

For printing to stdout and stderr. Similar to the console object functions provided by most web browsers, here the output is sent to stdout or stderr.

用于向 stdout 和 stderr 打印字符。类似于大部分 Web 浏览器提供的 console 对象函数,在这里则是输出到 stdout 或 stderr。

The console functions are synchronous when the destination is a terminal or a file (to avoid lost messages in case of premature exit) and asynchronous when it's a pipe (to avoid blocking for long periods of time).

当输出目标是一个终端或者文件时,console函数是同步的(为了防止过早退出时丢失信息).当输出目标是一个管道时它们是异步的(防止阻塞过长时间).

That is, in the following example, stdout is non-blocking while stderr is blocking:

也就是说,在下面的例子中,stdout 是非阻塞的,而 stderr 则是阻塞的。

$ node script.js 2> error.log | tee info.log

In daily use, the blocking/non-blocking dichotomy is not something you should worry about unless you log huge amounts of data.

在日常使用中,您不需要太担心阻塞/非阻塞的差别,除非您需要记录大量数据。

console.log([data], [...])#

Prints to stdout with newline. This function can take multiple arguments in a printf()-like way. Example:

向 stdout 打印并新起一行。这个函数可以像 printf() 那样接受多个参数,例如:

console.log('count: %d', count);

If formatting elements are not found in the first string then util.inspect is used on each argument. See util.format() for more information.

如果在第一个字符串中没有找到格式化元素,那么 util.inspect 将被应用到各个参数。详见 util.format()

console.info([data], [...])#

Same as console.log.

console.log

console.error([data], [...])#

Same as console.log but prints to stderr.

console.log,但输出到 stderr。

console.warn([data], [...])#

Same as console.error.

console.error

console.dir(obj)#

Uses util.inspect on obj and prints resulting string to stdout. This function bypasses any custom inspect() function on obj.

obj 使用 util.inspect 并将结果字符串输出到 stdout。这个函数会忽略 obj 上的任何自定义 inspect()

console.time(label)#

Mark a time.

标记一个时间点。

console.timeEnd(label)#

Finish timer, record output. Example:

结束计时器,记录输出。例如:

console.time('100-elements');
for (var i = 0; i < 100; i++) {
  ;
}
console.timeEnd('100-elements');

console.trace(label)#

Print a stack trace to stderr of the current position.

打印当前位置的栈跟踪到 stderr。

console.assert(expression, [message])#

Same as assert.ok() where if the expression evaluates as false throw an AssertionError with message.

assert.ok() 相同,如果 expression 执行结果为 false 则抛出一个带上 message 的 AssertionError。