nodejs入门_核心模块_全局变量
一、全局变量Process
(1) process.argv作用是什么?
(2) process.stdout.write 向标准输出打印字符串,比console.log()提供了更底层接口;
(3)process.stdin 标准输入流。初始时是被暂停,想要从标准输入流输入数据,必须通过process.stdin.resume()先恢复流,并手动写流的事件响应。
process.stdin.resume();
process.stdin.on('data',function(data){
process.stdout.write('read from console:'+data.toString());
});
为什么输入中文的时候,不打印出来?即使编码问题,应该也有输出呀?why?
(注意:这个是文件编码导致的问题。你把文件另存为utf-8的格式试试?)原因是存放的代码的文件格式有一次编码。导致中文解码错误,所以就不输出。
(4) process.nextTick(callback);
官方文档解释是:
This is not a simple alias to setTimeout(fn, 0), it's much more efficient. It runs before any additional I/O events (including timers) fire in subsequent ticks of the event loop.
意思是:这不仅仅是setTimeout(fn, 0)的别名,它比setTimeout更实用,性能更好。它发生在事件循环队列中任何I/O事件之前,包括定时器。
另外,对于事件中回调函数的执行,如:
function definitelyAsync(arg, cb) {
if (arg) {
process.nextTick(cb);
return;
}
fs.stat('file', cb);
}
使用process.nextTick()比使用cb()更好.官方说明是:
the nextTick queue is completely drained on each pass of the event loop before additional I/O is processed. As a result, recursively setting nextTick callbacks will block any I/O from happening, just like a while(true); loop.
意思是:
事件队列完全耗尽在 事件循环队列的每一个事件,在额外的I/O处理被处理前。结果,递归的设置nextTick回调函数将阻止所有的I/O事件发生,就像一个while(true)一样循环。
意思貌似表示的不是很清楚,但nextTick的作用如下:
1 不产生I/O等待,使事件像没有I/O事件一样继续执行;
2 就像一个环一样,nextTick里的内容轮询执行;
3 在所有的I/O 事件和定时器事件之前执行,即使有1000个或以上的nextTick回调函数而定时器的时间设置为0,都会在事件或者定时器之前执行;
二、console
1 console.log();
2 console.error();
3 console.trace();
三、常用工具util
1 util.inherits:继承方法,但不继承对象实例中的方法;
2 util.inspect:

浙公网安备 33010602011771号