NodeJs--process&util

全局对象

global
process
  • process.argv
    命令行参数数组 node 文件名 运行参数
//argv.js
console.log(process.argv)
//cmd
$ node argv.js 123 name=zhang --v
[ 'C:\\Program Files\\nodejs\\node.exe',
  'E:\\atom-demo\\helloworld\\argv.js',
  '123',
  'name=zhang',
  '--v' ]
  • process.stdout
    标准输出流,是比console.log的更底层实现
  • process.stdin
    标准输入流
  • process.nextTick
    setTimeout效率更高
function doSomething(args,callback){
  (function first(){
    console.log(args)
  })();
  callback();
}
doSomething('args',function call(){
  (function computed(){
    console.log("callback")
  })();
});
console.log("结束")
//args  callback  结束

function doSlowthing(args,callback){
  (function first(){
    console.log(args)
  })();
  process.nextTick(callback);
}
doSlowthing('args',function call(){
  (function computed(){
    console.log("callback")
  })();
});
console.log("结束")
//args  结束  callback
util
  • inherits
    继承,只继承prototype原型上的方法和属性
  • inspect
function Animal(){
  this.name = 'pig';
  this.toString = function(){
    return this.name
  }
}
var obj = new Animal()
console.log(util.inspect(obj,true))

Animal {
  name: 'pig',
  toString:
   { [Function]
     [length]: 0,
     [name]: '',
     [arguments]: null,
     [caller]: null,
     [prototype]: { [constructor]: [Circular] } } }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • https://nodejs.org/api/documentation.html 工具模块 Assert 测试 ...
    KeKeMars阅读 11,499评论 0 6
  • Node.js 全局对象 JavaScript 中有一个特殊的对象,称为全局对象(Global Object),它...
    FTOLsXD阅读 3,030评论 0 2
  • 内容来自《Node.js开发指南》 核心模块是 Node.js 的心脏,它由一些精简而高效的库组成,为 Node....
    angelwgh阅读 4,390评论 0 1
  • 前言: 因为以前学习Node.js并没有真正意义上的去学习它,而是粗略的学习了npm的常用命令和Node.js一些...
    Srtian阅读 4,572评论 1 17
  • 进程、线程 进程、线程是操作系统的基本概念 进程进程是正在运行的程序的实例,是一个具有一定独立功能的程序关于某个数...
    Sun晨淏阅读 4,235评论 0 2