除了官方文档,如何查看一个模块的下属方法、事件以及参数,因为看官方文档感觉好多都不全!我记得是有个是什么命令,但忘记了!请指教,谢谢!
11 回复
例如我要看預設模塊util有什麼function可以用, 可以這麼做
$ node
> var util = require('util');
undefined
> util
{ format: [Function],
deprecate: [Function],
print: [Function],
puts: [Function],
debug: [Function],
error: [Function],
inspect:
{ [Function: inspect]
colors:
{ bold: [Object],
italic: [Object],
underline: [Object],
inverse: [Object],
white: [Object],
grey: [Object],
black: [Object],
blue: [Object],
cyan: [Object],
green: [Object],
magenta: [Object],
red: [Object],
yellow: [Object] },
styles:
{ special: 'cyan',
number: 'yellow',
boolean: 'yellow',
undefined: 'grey',
null: 'bold',
string: 'green',
date: 'magenta',
regexp: 'red' } },
isArray: [Function: isArray],
isRegExp: [Function: isRegExp],
isDate: [Function: isDate],
isError: [Function: isError],
p: [Function: deprecated],
log: [Function],
exec: [Function: deprecated],
pump: [Function: deprecated],
inherits: [Function],
_extend: [Function] }
或是配合tab
> util.
util.__defineGetter__ util.__defineSetter__ util.__lookupGetter__ util.__lookupSetter__ util.constructor
util.hasOwnProperty util.isPrototypeOf util.propertyIsEnumerable util.toLocaleString util.toString
util.valueOf
util._extend util.debug util.deprecate util.error util.exec
util.format util.inherits util.inspect util.isArray util.isDate
util.isError util.isRegExp util.log util.p util.print
util.pump util.puts
如果要看懂一個 function 裡的參數做什麼, 你還是得看 function的整個程式碼, 或是他有寫註解了
function有幾個參數
> util.isDate.length
1
印出function
> util.isDate.toString()
'function isDate(d) {\n return typeof d === \'object\' && objectToString(d) === \'[object Date]\';\n}'
我的不會省略耶?
> net.Socket.toString()
'function Socket(options) {\n if (!(this instanceof Socket)) return new Socket(options);\n\n this._connecting = false;\n this._handle = null;\n\n switch (typeof options) {\n case \'number\':\n options = { fd: options }; // Legacy interface.\n break;\n case \'undefined\':\n options = {};\n break;\n }\n\n stream.Duplex.call(this, options);\n\n if (options.handle) {\n this._handle = options.handle; // private\n } else if (typeof options.fd !== \'undefined\') {\n this._handle = createPipe();\n this._handle.open(options.fd);\n this.readable = options.readable !== false;\n this.writable = options.writable !== false;\n } else {\n // these will be set once there is a connection\n this.readable = this.writable = false;\n }\n\n this.onend = null;\n\n // shut down the socket when we\'re finished with it.\n this.on(\'finish\', onSocketFinish);\n this.on(\'_socketEnd\', onSocketEnd);\n\n initSocketHandle(this);\n\n this._pendingData = null;\n this._pendingEncoding = \'\';\n\n // handle strings directly\n this._writableState.decodeStrings = false;\n\n // default to *not* allowing half open sockets\n this.allowHalfOpen = options && options.allowHalfOpen || false;\n\n // if we have a handle, then start the flow of data into the\n // buffer. if not, then this will happen when we connect\n if (this._handle && options.readable !== false)\n this.read(0);\n}'