process.stdout(); //標準輸出流
process.stdout.write() //提供了比console.log更底層的接口
process.stdin(); //標準輸入流
//初始時他是被暫停的,要想從他那裏讀取數據須要恢復流
//而且手動編寫流的事件響應函數
process.stdin.resume();
process.stdin.on('data',function(data){
process.stdout.write('smile:'+data.toString());
})
//爲事件循環設置一項任務,node會在下一次循環的時候調用callback
process.nextTick(callback);
//假設a,b兩個函數較爲耗時
function a(argv,callback){
b(argv);
callback();
}
a(function(){
n();
})
//用process.nextTick()改寫
function a(argv,callback){
b(argv);
process.nextTick(callback);
}
a(function(){
n();
})
//process.nextTick()的效率要比setTimeout(callback,0)要高不少
//util是node.js的核心模塊,用於彌補javascript的不足
//util.inherits(constructor,superConstructor)是一個實現對象間原型繼承的函數
//javascript沒有提供對象繼承的語言級別特性,而是經過原型複製來實現的
var util = require('util');
var base = function(){
this.name = 'base';
this.sayHello = function(){
console.log('hello,'+this.name);
}
}
base.prototype.showName = function(){
console.log(this.name);
}
var sub = function(){
this.name = 'sub';
}
util.inherits(sub,base);
var objBase = new base();
var objSub = new sub();
objBase.sayHello(); //hello,base
objBase.showName(); //base
console.log(objBase); // < name:base , sayHello:[Function] >
objSub.showName(); //sub
console.log(objSub); // < name:sub>
//inherits會繼承prototype中的函數但不會繼承內部的屬性;