如何查看一个模块的下属方法、事件以及参数
发布于 11 年前 作者 didiaoyu 5433 次浏览 最后一次编辑是 8 年前

除了官方文档,如何查看一个模块的下属方法、事件以及参数,因为看官方文档感觉好多都不全!我记得是有个是什么命令,但忘记了!请指教,谢谢!

11 回复

去 node_modules 下看 module 的程序源码

为什么我的安装目录下的C:\Program Files\nodejs\node_modules\下就只有npm一个文件夹,没有安装时自带的那些模块呢?

@didiaoyu 安装的时候失败了

例如我要看預設模塊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的参数的办法呢? 谢谢!

@zhs077 没有呀,我运行都很正常的!

@didiaoyu

如果要看懂一個 function 裡的參數做什麼, 你還是得看 function的整個程式碼, 或是他有寫註解了

function有幾個參數

> util.isDate.length
1

印出function

> util.isDate.toString()
'function isDate(d) {\n  return typeof d === \'object\' && objectToString(d) === \'[object Date]\';\n}'

@hankwang 在交互模式下,toString()打印出的方法常常是省略号加行号,看不全

@hankwang 嗯,好的,很好用,谢谢!

@jimokanghanchao

我的不會省略耶?

> 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}'

@hankwang ^^ 说错了,准确来讲是交互模式不省略, node debug模式时,执行repl,toString打印的方法是省略的。

回到顶部