課程
/前端開發(fā)
/Node.js
/進(jìn)擊Node.js基礎(chǔ)(二)
fs模塊創(chuàng)建的流和stream創(chuàng)建的可讀可寫流有什么區(qū)別嗎,還有_write,_read這些到底是什么?
2016-05-14
源自:進(jìn)擊Node.js基礎(chǔ)(二) 2-4
正在回答
_write,_read 是 私有函數(shù)的名字。 這些流具體怎么讀 怎么寫,就是通過修改各自類原型的私有方法來實現(xiàn)的
比如:
ReadStream.prototype._read = function(){
? ? this.push(' ...')
???? ...
}
// abstract method. ?to be overridden in specific implementation classes. ? ?
// call cb(er, data) where data is <= n in length. ? ?
// for virtual (non-string, non-buffer) streams, "length" is somewhat ? ?
// arbitrary, and perhaps not very meaningful. ? ?
Readable.prototype._read = function(n) { ? ?
this.emit('error', new Error('not implemented')); ? ?
}; ? ?https://github.com/nodejs/node/blob/master/lib/_stream_readable.js
這是他的解釋,它是readable里的一個抽象方法,這里是對這個抽象方法進(jìn)行了實現(xiàn),而fs.js里面已經(jīng)對這個方法實現(xiàn)過了
fs.createReadStream?=?function(path,?options)?{???? return?new?ReadStream(path,?options);???? };???? ----------------- ReadStream.prototype._read?=?function(n)?{???? if?(typeof?this.fd?!==?'number')???? return?this.once('open',?function()?{???? this._read(n);???? });???? if?(this.destroyed)???? return;???? if?(!pool?||?pool.length?-?pool.used?<?kMinPoolSpace)?{???? //?discard?the?old?pool.???? pool?=?null;???? allocNewPool(this._readableState.highWaterMark);???? }
代碼是部分的,大概是告訴你這是真的
https://github.com/nodejs/node/blob/master/lib/fs.js
有興趣自己去看看吧。
舉報
本教程帶你攻破 Nodejs,讓 JavaScript流暢運行在服務(wù)器端
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網(wǎng)安備11010802030151號
購課補貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號
2017-02-21
_write,_read 是 私有函數(shù)的名字。 這些流具體怎么讀 怎么寫,就是通過修改各自類原型的私有方法來實現(xiàn)的
比如:
ReadStream.prototype._read = function(){
? ? this.push(' ...')
???? ...
}
2016-05-16
// abstract method. ?to be overridden in specific implementation classes. ? ?
// call cb(er, data) where data is <= n in length. ? ?
// for virtual (non-string, non-buffer) streams, "length" is somewhat ? ?
// arbitrary, and perhaps not very meaningful. ? ?
Readable.prototype._read = function(n) { ? ?
this.emit('error', new Error('not implemented')); ? ?
}; ? ?
https://github.com/nodejs/node/blob/master/lib/_stream_readable.js
這是他的解釋,它是readable里的一個抽象方法,這里是對這個抽象方法進(jìn)行了實現(xiàn),而fs.js里面已經(jīng)對這個方法實現(xiàn)過了
代碼是部分的,大概是告訴你這是真的
https://github.com/nodejs/node/blob/master/lib/fs.js
有興趣自己去看看吧。