核心模块主要内容:
全局对象
常用工具
事件机制
文件系统访问
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路相扶


node.js global object,util and so on的更多相关文章

  1. Node.js:常用工具util

    概要:本篇博客的主要内容是介绍node.js的常用工具util. 1.util.inherits util.inherits(constructor,superConstructor)是一个实现对象间 ...

  2. java结合node.js非对称加密,实现密文登录传参——让前后端分离的项目更安全

    前言   在参考互联网大厂的登录.订单.提现这类对安全性操作要求较高的场景操作时发现,传输的都是密文.而为了目前项目安全,我自己负责的项目也需要这方面的技术.由于,我当前的项目是使用了前后端分离技术, ...

  3. Node.js 常用工具 (util.inherits)

    util 是一个Node.js 核心模块,提供常用函数的集合,用于弥补核心JavaScript 的功能 过于精简的不足. util.inherits util.inherits(constructor ...

  4. Node.js+Web TWAIN,实现Web文档扫描和图像上传

      目录(?)[+] 通过Dynamic Web TWAIN SDK和Node.js的组合,只需要几行代码就可以实现在浏览器中控制扫描仪,获取图像后上传到远程服务器. 原文:Document Imag ...

  5. The Node.js Event Loop, Timers, and process.nextTick() Node.js事件循环,定时器和process.nextTick()

    个人翻译 原文:https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/ The Node.js Event Loop, Ti ...

  6. 关于node.js和npm,cnpm的安装记录以及gulp自动构建工具的使用

    关于node.js和npm,cnpm的安装记录以及gulp自动构建工具的使用   工作环境:window下 在一切的最开始,安装node.js (中文站,更新比较慢http://nodejs.cn/) ...

  7. GitBook是一个命令行工具(Node.js库),我们可以借用该工具使用Github/Git和Markdown来制作精美的图书,但它并不是一本关于Git的教程哟。

    GitBook是一个命令行工具(Node.js库),我们可以借用该工具使用Github/Git和Markdown来制作精美的图书,但它并不是一本关于Git的教程哟. 支持输出多种格式 GitBook支 ...

  8. 打算写一个《重学Node.js》系列,希望大家多多支持

    先放上链接吧,项目已经开始2周了:https://github.com/hellozhangran/happy-egg-server 想法 现在是2019年11月24日,还有人要开始学习Node.js ...

  9. 你不知道的Node.js性能优化,读了之后水平直线上升

    本文由云+社区发表 "当我第一次知道要这篇文章的时候,其实我是拒绝的,因为我觉得,你不能叫我写马上就写,我要有干货才行,写一些老生常谈的然后加上好多特技,那个 Node.js 性能啊好像 D ...

随机推荐

  1. Eclipse的下载及安装

    Eclipse的下载地址: https://www.eclipse.org/downloads/ 下载完成后,双击安装包即可安装 选择 Eclipse IDE for Java EE Decelope ...

  2. Mac虚拟机

    2018-06-21 需要的Mac静像是ios或是cdr的,如果是dmg,可以参考这个转换http://blog.sina.com.cn/s/blog_60b45f230101kkbf.html 或  ...

  3. IOS代码片段

    2017-08-03 获取应用程序委托FKAppDelegate* appDelegate = [UIApplication shareApplication].delegate 2017-08-03 ...

  4. Java设计模式(8)——策略模式

    一.策略模式定义 Strategy模式也叫策略模式是行为模式之一,它对一系列的算法加以封装,为所有算法定义一个抽象的算法接口,并通过继承该抽象算法接口对所有的算法加以封装和实现,具体的算法选择交由客户 ...

  5. django 静态文件

    django中的静态文件,如图片,css样式jquery等等 在url最下面加上 from django.conf.urls.static import staticfrom django.conf ...

  6. OneZero第三周第四次站立会议(2016.4.7)

    1. 时间: 18:35--18:50 共计15分钟. 2. 成员: X 夏一鸣 * 组长 (博客:http://www.cnblogs.com/xiaym896/), G 郭又铭 (博客:http: ...

  7. GC收集器种类

    转载:https://wangkang007.gitbooks.io/jvm/content/la_ji_shou_ji_qi.html 收集器 1.1 Serial(串行)收集器 Serial收集器 ...

  8. spring学习 十三 注解AOP

    spring 不会自动去寻找注解,必须告诉 spring 哪些包下的类中可能有注解,也就是要开启注解扫描,注解的包是spring-context.jar,所以在配置文件中还要引入context约束,也 ...

  9. 解决SecureCRT超时自动断开的问题

    http://blog.csdn.net/hcwzq/article/details/7944941. http://discuzx.sinaapp.com/mediawiki-chapter.htm ...

  10. 680. Valid Palindrome II

    static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...