首页 > 代码库 > node对象

node对象

global,node的全局对象;js在游览器中的全局对象为windows

在node环境中;输入global.consloe

输出:

Console {
  log: [Function: bound ],
  info: [Function: bound ],
  warn: [Function: bound ],
  error: [Function: bound ],
  dir: [Function: bound ],
  time: [Function: bound ],
  timeEnd: [Function: bound ],
  trace: [Function: bound trace],
  assert: [Function: bound ],
  Console: [Function: Console] }

process也是node的一个对象,代表当前node进程
> process === global.process;
true
> process.version;
‘v5.2.0‘
> process.platform;
‘darwin‘
> process.arch;
‘x64‘
> process.cwd(); //返回当前工作目录
‘/Users/michael‘
> process.chdir(‘/private/tmp‘); // 切换当前工作目录
undefined
> process.cwd();
‘/private/tmp‘
js是由事件驱动执行的单线程模式;node也不例外
node不断执行响应的js函数:执行完就退出了
如果要下一次响应中执行代码
可以调用process.nextTick()
// test.js

// process.nextTick()将在下一轮事件循环中调用:
process.nextTick(function () {
    console.log(‘nextTick callback!‘);
});
console.log(‘nextTick was set!‘);
打印输出
nextTick was set!
nextTick callback!
说明
process.nextTick()不是立即执行,而是要等到下一次循环
Node进程本身的事件就是由process来处理
判断js执行环境:
根据游览器和node提供的全局变量来判断:
if (typeof(window) === ‘undefined‘) {
    console.log(‘node.js‘);
} else {
    console.log(‘browser‘);
}
 
 


 

node对象