码迷,mamicode.com
首页 > Web开发 > 详细

nodeJs入门笔记(二)

时间:2015-10-29 23:22:31      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:

 

js中window通常是全局变量

global 是node.js里的全局变量 node中能访问的对象一般都是 global的 属性

global 对象属性 process 用于描述当前Node 进程状态的对象,提供了一个与操作系统的简单接口.

process.argv :命令行参数数组,第一个元素是 node,第二个元素是脚本文件名,从第三个元素开始每个元素是一个运行参数

console.log(process.argv);

$ node argv.js 1991 name=byvoid --v "Carbo Kuo" 
[ node, 
/home/byvoid/argv.js, 
1991, 
name=byvoid, 
--v, 
Carbo Kuo ] 

process.stdout.write() 通常我们调试 是用console.log() 而它提供了更底层的接口

process.nextTick(callback) 作用给功能增加一个新的项目,node会在下次循环响应时候调用callback

我个人理解就是跟js的 回调函数一个样子 比如:

//js的写法
function a(b,callback){
    c(b);
    callback()
} 

a("lee",function(){

     alert("lee是帅哥")    
});

//用nextTick改写
function a(b,callback){
    c(b);
    process.nextTick(callback)
} 

a("lee",function(){

     alert("lee是帅哥")    
});

但是我相信一门语言 出现每个的方法 都有属于自己独特的魅力 更加具体的效果我也不知道怎么体现 路过的大神望解答

 

util 是node的核心模块。

util.inherits() 是实现对象之间的原型继承 比如说:

var util = require("util");

function lee(){
    this.name = "lee";
    this.time = "1991";
    this.leeshuo = function(){
        console.log("hello i am " +this.name)
    }
    
}
lee.prototype.aaa= "aini"
lee.prototype.kx = function(){
    console.log("i am "+this.name);
}


function Hmx(){
    this.name="mx";
}

util.inherits(Hmx,lee);

var lee = new lee();


lee.leeshuo();
lee.kx();
console.log(lee)
var hmx = new Hmx();    

console.log(hmx.aaa);
hmx.kx();

看到了吧 hmx 继承了 lee的prototpye ..话说 这方法还挺好用的..

util.inspect(object,[showHidden],[depth],[colors]) 把对象转成字符串 通常用于调试和错误输出. 至少接受一个参数 object,

showHidden 是一个可选参数,如果值为 true,将会输出更多隐藏信息。
depth 表示最大递归的层数,如果对象很复杂,你可以指定层数以控制输出信息的多
少。如果不指定depth,默认会递归2层,指定为 null 表示将不限递归层数完整遍历对象。
如果color 值为 true,输出格式将会以ANSI 颜色编码,通常用于在终端显示更漂亮
的效果。

var util = require("util");

function lee(){
	this.name = "lee";
	this.time = "1991";
	this.leeshuo = function(){
		return this.name;
	}
	
}

var lee = new lee();
console.log(util.inspect(lee))
console.log(util.inspect(lee,true,2,true))


//输出内容
lee { name: ‘lee‘, time: ‘1991‘, leeshuo: [Function] }
lee {
  name: ‘lee‘,
  time: ‘1991‘,
  leeshuo:
   { [Function]
     [length]: 0,
     [name]: ‘‘,
     [arguments]: null,
     [caller]: null,
     [prototype]: { [constructor]: [Circular] } } }

events 是Node.js 最重要的模块,没有“之一”,原因是Node.js 本身架构就是事件式的,而它提供了唯一的接口,所以堪称Node.js 事件编程的基石

events 模块只提供了一个对象: events.EventEmitter    每个事件由一个事件名和若干个参数组成,事件名是一个字符串

直接上代码

var events = require("events");
var emitter = new events.EventEmitter();

emitter.on("onevent",function(a,b){
    console.log("1111 " + a +" hao "+ b);
});
emitter.on("onevent",function(a,b){
    console.log("2222 " + a +" hao "+ b);
});
//发射
emitter.emit("onevent","lee","shuai");

 

node还绑定了一个特别的事件 "error"  应用的场景:

我们在遇到
异常的时候通常会发射 error 事件。当 error 被发射时,EventEmitter 规定如果没有响
应的监听器,Node.js 会把它当作异常,退出程序并打印调用栈。我们一般要为会发射 error
事件的对象设置监听器,避免遇到错误后整个程序崩溃

emitter.emit("error")

 

继续成长.....

(完)

 

nodeJs入门笔记(二)

标签:

原文地址:http://www.cnblogs.com/lee-iekiller/p/4922063.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!