node.js global object,util and so on

时间:2023-03-09 03:31:08
node.js global object,util and so on
 核心模块主要内容:
全局对象
常用工具
事件机制
文件系统访问
http服务器和客户端 global object:
所有的全局变量(除了global本身外)都是global object 的属性(attribute) global object,global variable global的根本作用就最为全局变量的宿主:
what is global variable?:
在最外层定义的
全局对象的属性
隐式定义的变量(未定义直接赋值的变量) Notice: 在Node.js中,你不可能在最外层定义变量,因为所有的用户代码都是当前模块的,二模块本身不是最外层上下文 var 声明的不是全局变量 process ->global variable //Desc the current of node.js process status
process.argv是命令行参数数组,第一个元素是node,第二个是脚本文件名,第三个是参数 //for example
/*
*author:e路相扶
*filename argv.js
*/
console.log(process.argv);
$ node argv.js name=eself --v 'zhangjun' output:
['node','/home/argv.js','name=eself','--v','zhangjun']; process.stdout是标准的输出流,
process.stdout.write()函数提供了更底层的接口
process.stdin是标准的输入流,初始时,她是被暂停的,要想从标准输入读取数据,必须恢复流,并且手动编写流的事件响应函数 /*
*author:e路相扶
*filename stdin.js
*/
process.stdin.resume();
process.stdin.on('data',function(data)){
process.stdout.write('read from console :'+ data.toStrin());
} process.nextTick(callback)的功能是为事件循环设置一项任务,node.js会在事件循环响应时调用callback
node.js的一个编程原则是尽量缩短每个事件的执行时间,process.nickTick()提供了这样的工具
 /*
*author:e路相扶
*filename nextTick.js
*/ function doSomething(args,callback){
somethingComplicated(args);
process.nextTick(callback);
}
doSomething(function onEnd(){
compute();
});
-----------------------------------------
Notice :不要使用setTimeout(fn,)代替process.nextTick(callback),前者比后者的效率要低很多 process还有很多成员,详细地址查看 http://nodejs.org/api/process.html console :
console用于提供控制台标准的输出,
console.log(variable),如果一个参数,则输出这个参数的字符串形式,多个参数,则参照c的printf()命令的格式
//js中也有这个,比alert()测试好用--个人感觉
console.error() :the same as console.log() 只是像标准错误流输出
console.trace()向标准错误流输出当前的调用栈 Util://提供常用函数的集合
util.inherits(constructor,superConstructor)是一个实现对象间原型继承的函数
/*
*author:e路相扶
*filename util.js
*/ var util=require('util');
function Base(){
this.name='base';
this.base='';
this.sayHello=function(){
console.log('Hello '+this.name);
}
}
Base.prototype.showName=function(){
console.log(this.name);
}
function sub(){
this.name='sub';
}
util.inherits(sub,Base);
var objBase=new Base();
objBase.showName();// base
objBase.sayHello();//hello base
console.log(objBase);//{name:'base',base:1991,sayHello:[function]} sub {name:'sub'}
 util.inspect(object,[showHidden],[depth],[colors]) 是一个将任意对象转换为字符串的方法,通常用于调试和错误输出
object:将要转换的对象
showHidden:如果是true,将会输出更多隐藏信息
depth:表示最大递归层数,不指定默认为递归2层,指定为null表示不限递归层数,完整便利对象
color:true输出格式将会以ANSI颜色编码
util.inspect并不会简单地直接把对象转换为字符串,即使该对象定义了toString方法也不会调用。
/*
*author:e路相扶
*filename inspect.js
*/
var util=require('util');
function Person(){
this.name='e路相扶';
this.toString=function(){
return this.name;
}
}
var obj=new Person();
console.log(util.inspect(obj));
console.log(util.inspect(obj,true)); output:
{name:'e路相扶',toString:[function]}
{toString:
{[function]
[prototype]:{[constructor]:[Circular]},
[caller]:null,
[length]:,
[name]:'',
[arguments]:null},
name:'e路相扶'
} 除了上面几个函数,util.isArray(),util.isRegExp(),util.isDate(),util.isError(),util.format(),
util.debug()等我们可以在http://nodejs.org/api/util.html 了解详细内容 event-driven events:
events 是Node.js最重要的模块,没有之一,Node.js本身架构就是事件式的
事件发射器:
events模块只能提供一个对象,events.EventEmitter
events.EventEmitter核心就是事件发射与事件监听功能的封装,EventEmitter的每个事件就是由一个事件名和
若干个参数组成,事件名是一个字符串,通常表达一定的语义。对于每个事件,EventEmitter支持若干个事件监听器
当事件发射时,注册到这个事件的事件监听器被一次调用,事件参数为回调函数参数传递
/*
*author:e路相扶
*filename events.js
*/
var events=require('events');
var emitter=new events.EventEmitter();
emitter.on('someEvent',function(arg1,arg2){
console.log('listener1',arg1,arg2);
});
emitter.on('someEvent',function(arg1,arg2){
console.log('listener2',arg1,arg2);
});
emitter.emit('someEvent','e路相扶',); output:
listener1 e路相扶
listener2 e路相扶