These functions are in the module 'util'
. Use require('util')
to access
them.
下列函数属于'util'
(工具)模块,可使用require('util')
访问它们。
A synchronous output function. Will block the process and
output string
immediately to stderr
.
这是一个同步输出函数,将string
参数的内容实时输出到stderr
标准错误。调用此函数时将阻塞当前进程直到输出完成。
require('util').debug('message on stderr');
Output with timestamp on stdout
.
将string
参数的内容加上当前时间戳,输出到stdout
标准输出。
require('util').log('Timestmaped message.');
Return a string representation of object
, which is useful for debugging.
以字符串形式返回object
对象的结构信息,这对程序调试非常有帮助。
If showHidden
is true
, then the object's non-enumerable properties will be
shown too.
如果showHidden
参数设置为true
,则此对象的不可枚举属性也会被显示。
If depth
is provided, it tells inspect
how many times to recurse while
formatting the object. This is useful for inspecting large complicated objects.
可使用depth
参数指定inspect
函数在格式化对象信息时的递归次数。这对分析复杂对象的内部结构非常有帮助。
The default is to only recurse twice. To make it recurse indefinitely, pass
in null
for depth
.
默认情况下递归两次,如果想要无限递归可将depth
参数设为null
。
Example of inspecting all properties of the util
object:
显示util
对象所有属性的例子如下:
var util = require('util');
console.log(util.inspect(util, true, null));
Experimental
实验性的
Read the data from readableStream
and send it to the writableStream
.
When writableStream.write(data)
returns false
readableStream
will be
paused until the drain
event occurs on the writableStream
. callback
gets
an error as its only argument and is called when writableStream
is closed or
when an error occurs.
从readableStream
参数所指定的可读流中读取数据,并将其写入到writableStream
参数所指定的可写流中。当writeableStream.write(data)
函数调用返回为false
时,readableStream
流将被暂停,直到在writableStream
流上发生drain
事件。当writableStream
流被关闭或发生一个错误时,callback
回调函数被调用。此回调函数只接受一个参数用以指明所发生的错误。
Inherit the prototype methods from one
constructor
into another. The prototype of constructor
will be set to a new
object created from superConstructor
.
将一个构造函数的原型方法继承到另一个构造函数中。constructor
构造函数的原型将被设置为使用superConstructor
构造函数所创建的一个新对象。
As an additional convenience, superConstructor
will be accessible
through the constructor.super_
property.
此方法带来的额外的好处是,可以通过constructor.super_
属性来访问superConstructor
构造函数。
var util = require("util");
var events = require("events");
function MyStream() {
events.EventEmitter.call(this);
}
util.inherits(MyStream, events.EventEmitter);
MyStream.prototype.write = function(data) {
this.emit("data", data);
}
var stream = new MyStream();
console.log(stream instanceof events.EventEmitter); // true
console.log(MyStream.super_ === events.EventEmitter); // true
stream.on("data", function(data) {
console.log('Received data: "' + data + '"');
})
stream.write("It works!"); // Received data: "It works!"