这两种方法——console.log和process.stdout.write都有一个基本定义来在控制台上写入或打印语句。但是,它们执行这些任务的方式略有不同。在内部,console.log实现了process.stdout.write,它本身是一个缓冲区流,用于直接在控制台上打印语句。
process.stdout.write
| 控制台日志
| Itcontinuouslyprintsinformationasretrievedfromthestreamwithoutaddinganynewline.
| 它首先打印正在检索的信息,然后添加一个新行。然后它将去检索要打印的第二组语句。
|
Theprocess.stdout.writemethodtakesonlystringasparamter.Otherdatatypespassedwillresultinatypeerror.
| 它可以采用任何类型的输入参数。
|
Wewillgetaweirdcharactedifwedon'tputthebreaklineattheend.
| 我们在这里不需要任何换行,因为文本已经格式化,字符也会自行消失。
|
Itisusedforprintingpatterns,sinceitdoesnotaddsanewline.
| 当我们在打印语句后需要换行时使用它。
|
Itcannotbeusedtoprintmultiplestringsatatime. Forexample,process.stdout.write("Hello","World");willthrowatypeerrorasitisnotastringanymore.
| 我们可以使用这种方法编写多个字符串。例如,console.log("Hello","World");将在控制台中打印HelloWorld。
|
Thismethodcannotassociatetwostringtypes.Forexample, process.stdout.write("Hello%s","All");willthrowatypeerror/
| 此方法可用于建立关联。 例如,console.log("Hello%s","All");将在控制台上打印HelloAll。
|
process.stdout.write
示例1
输出结果HelloWorld!!!
console.log
示例2
输出结果Hello
World
!!!