2011-02-12 141 views
185

node.js中「process.stdout.write」和「console.log」之間的區別是什麼?node.js中「process.stdout.write」和「console.log」的區別?

編輯:使用console.log爲變量顯示了很多不可讀的字符,同時使用process.stdout.write顯示一個對象。

這是爲什麼?

+3

你能提供一個例子嗎? console.log()使用格式化輸出調用process.stdout.write。有關實現,請參閱console.js中的format()。 – 2011-02-12 11:47:53

+1

發佈它作爲答案=) – ajsie 2011-02-13 03:34:46

回答

176

console.log()調用process.stdout.write格式化輸出。有關實現,請參閱console.js中的format()

目前(v0.10.ish):

Console.prototype.log = function() { 
    this._stdout.write(util.format.apply(this, arguments) + '\n'); 
}; 
107

望着節點文檔顯然CONSOLE.LOG只是在末端的線斷process.stdout.write:

console.log = function (d) { 
    process.stdout.write(d + '\n'); 
}; 

來源:http://nodejs.org/docs/v0.3.1/api/process.html#process.stdout

+9

對於後來出現的人來說,請注意,v0.3.1是很久以前的事情,並且自那以後發生了變化。 :) – 2015-02-25 04:17:58

+4

...不。剛剛查看了v0.9.9文檔,console.log仍然是process.stdout.write的別名,並帶有換行符。請在評論之前做你的研究。 http://nodejs.org/docs/v0.9.9/api/process.html#process.stdout – 2015-02-25 17:53:05

38

我知道這是一個非常古老的問題,但我沒有看到任何人談論process.stdout.writeconsole.log之間的主要區別,我只是想給我注意它。

由於Mauvis LefordTK-421指出的,console.log在行(\n)的末尾添加一個line-break字符,但還不是全部它做什麼。

自從至少有0.10.X版本以來,代碼沒有改變,現在我們有一個5.X版本。

Here是代碼:

Console.prototype.log = function() { 
    this._stdout.write(util.format.apply(this, arguments) + '\n'); 
}; 

正如你所看到的,是說.apply(this, arguments)的一部分,這使得功能上有很大的區別。這是比較容易解釋,舉例:

process.stdout.write有一個非常基本的功能,你可以只寫在那裏的東西,像這樣:

process.stdout.write("Hello World\n"); 

如果你不把斷線末您的字符串後,你會得到一個奇怪的字符,像這樣:

process.stdout.write("Hello World"); //Hello World% 

(我認爲這意味着類似「節目的結束」,所以你會看到它只有在你process.stdout.write是在使用你的文件的結尾,你沒有' t加分行)

另一方面,console.log可以做得更多。

  1. 您可以使用它以同樣的方式

    console.log("Hello World"); //You don't need the break line here because it was already formated同時又有怪異的性格也消失

  2. 你可以寫個以上的字符串

    console.log("Hello", "World");

  3. 你可以使協會

    console.log("Hello %s", "World") //Useful when "World" is inside a variable

的就是這樣,那增加的功能被賦予感謝util.format.apply部分(我可以講了很多關於究竟這樣做,但你明白我的意思,你可以閱讀更多here)。

我希望有人發現這個信息有用。

17

沒有提到的一個很大的區別是process.stdout只將字符串作爲參數(也可以是管道流),而console.log採用任何Javascript數據類型。

e.g:

// ok 
console.log(null) 
console.log(undefined) 
console.log('hi') 
console.log(1) 
console.log([1]) 
console.log({one:1}) 
console.log(true) 
console.log(Symbol('mysymbol')) 

// any other data type passed as param will throw a TypeError 
process.stdout.write('1') 

// can also pipe a readable stream (assuming `file.txt` exists) 
const fs = require('fs') 
fs.createReadStream('file.txt').pipe(process.stdout) 
1

我剛剛發現了一些,同時幫助您使用https.request爲POST方法後,研究這個。以爲我分享一些幫助理解的意見。

process.stdout.write不會添加新行,而console.log會像其他人一樣提到。但也有一些更容易用例子來解釋的。

var req = https.request(options, (res) => { 
    res.on('data', (d) => { 
     process.stdout.write(d); 
     console.log(d) 
    }); 
}); 

process.stdout.write(d);將正確打印數據而不會換行。但是console.log(d)將會打印一個新行,但數據不會正確顯示,例如<Buffer 12 34 56...

要讓console.log(d)正確顯示信息,我必須這樣做。

var req = https.request(options, (res) => { 
    var dataQueue = "";  
    res.on("data", function (d) { 
     dataQueue += d; 
    }); 
    res.on("end", function() { 
     console.log(dataQueue); 
    }); 
}); 

所以基本上:

  • process.stdout.write連續打印的信息作爲被檢索的數據,並沒有增加新的線路。

  • console.log打印檢索點獲得的信息並添加新行。

這是我能解釋它的最好方法。

2

這方面的另一個重要區別是process.stdout.clearLine()process.stdout.cursorTo(0)

如果您想在唯一一行中顯示下載或處理的百分比,這將非常有用。如果使用clearLine(),cursorTo()與console.log()不起作用,因爲它也會將\ n附加到文本中。試試這個例子:

var waitInterval = 500; 
var totalTime = 5000; 
var currentInterval = 0; 

function showPercentage(percentage){ 
    process.stdout.clearLine(); 
    process.stdout.cursorTo(0); 
    console.log(`Processing ${percentage}%...`); //replace this line with process.stdout.write(`Processing ${percentage}%...`); 
} 

var interval = setInterval(function(){ 
currentInterval += waitInterval; 
showPercentage((currentInterval/totalTime) * 100); 
}, waitInterval); 

setTimeout(function(){ 
clearInterval(interval); 
}, totalTime); 
相關問題