"process.stdout. "在node.js中写入“和”console.log“?
EDIT:使用console.log作为变量显示了许多不可读的字符,而使用process.stdout.write显示了一个对象。
为什么呢?
"process.stdout. "在node.js中写入“和”console.log“?
EDIT:使用console.log作为变量显示了许多不可读的字符,而使用process.stdout.write显示了一个对象。
为什么呢?
当前回答
对于那些喜欢Deno的人,我可以通过使用下面的ANSI转义序列和Deno版本的process.stdout.write来实现这一点。
ESC[2K clears entire line
ESC[#G moves cursor to column #
Code
/*
Console.log() to the same line
Supported in Deno 1.8.1
*/
const myTextEncoder : TextEncoder = new TextEncoder();
let counter : number = 0;
while(counter < 100000) {
// ESC[2K clears entire line
await Deno.stdout.write(myTextEncoder.encode(`\x1b[2K`));
// ESC[#G moves cursor to column #
await Deno.stdout.write(myTextEncoder.encode(`\x1b[0G`));
// OUTPUT incremented counter
await Deno.stdout.write(myTextEncoder.encode(`COUNTER: ${counter++}`));
}
其他回答
这个上下文中另一个重要的区别是process.stdout.clearLine()和process.stdout.cursorTo(0)。
如果您想在一行中显示下载或处理的百分比,这将非常有用。如果你使用clearLine(), cursorTo()与console.log(),它不会工作,因为它也附加\n到文本。试试这个例子:
var totalTime = 5000;
var waitInterval = totalTime / 10;
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 + 100)
查看Node文档,显然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
Console.log()使用格式化输出调用process.stdout.write。具体实现请参见console.js中的format()。
目前(v0.10.ish):
Console.prototype.log = function() {
this._stdout.write(util.format.apply(this, arguments) + '\n');
};
我知道这是一个非常老的问题,但我没有看到任何人谈论process.stdout.write和console.log之间的主要区别,我只是想提一下。
正如Mauvis Leford和TK-421指出的那样,console.log在行末添加了一个换行字符(\n),但这并不是它的全部功能。
该代码至少从0.10开始就没有更改过。X版本,现在我们有一个5。X版本。
代码如下:
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被用在你的文件的末尾,你没有添加断行)
另一方面,console.log可以做更多的工作。
你可以用同样的方法来使用它 console.log(“Hello World”);//这里不需要换行符,因为换行符已经设置好了,而且那个奇怪的字符也消失了 你可以写多个字符串 console.log(“你好”、“世界”); 你可以进行联想 console.log("Hello %s", "World") //当"World"在变量中时有用
就是这样,添加的功能要感谢util.format.apply部分(我可以谈论很多关于它到底做了什么,但你明白我的意思,你可以在这里阅读更多)。
我希望有人觉得这个信息有用。
对于那些喜欢Deno的人,我可以通过使用下面的ANSI转义序列和Deno版本的process.stdout.write来实现这一点。
ESC[2K clears entire line
ESC[#G moves cursor to column #
Code
/*
Console.log() to the same line
Supported in Deno 1.8.1
*/
const myTextEncoder : TextEncoder = new TextEncoder();
let counter : number = 0;
while(counter < 100000) {
// ESC[2K clears entire line
await Deno.stdout.write(myTextEncoder.encode(`\x1b[2K`));
// ESC[#G moves cursor to column #
await Deno.stdout.write(myTextEncoder.encode(`\x1b[0G`));
// OUTPUT incremented counter
await Deno.stdout.write(myTextEncoder.encode(`COUNTER: ${counter++}`));
}