Javascript node.js 中 console.log 和 sys.puts 的区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3904273/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 06:35:05  来源:igfitidea点击:

Difference between console.log and sys.puts in node.js?

javascriptconsolenode.js

提问by never_had_a_name

In node.js you can use console.logor sys.putsto print out to the screen.

在 node.js 中,您可以使用console.logsys.puts打印到屏幕上。

What is the preferred method and what is the difference between these?

什么是首选方法,它们之间有什么区别?

回答by Elias

sys.putssimply prints the given string in the logs.

sys.puts简单地在日志中打印给定的字符串。

But if you want to print a more complex object (Array, JSON, JSObject) you have to use console.logbecause you want to "look inside" of the object.

但是如果你想打印一个更复杂的对象(数组、JSON、JSObject),你必须使用,console.log因为你想“查看”对象的内部。

sys.putswould give you only "[object object]" for example.

sys.puts例如,只会给你“[object object]”。

回答by isaacs

Both just write to the stdout stream. The difference is that sys.putsjust toString's the first argument, and console.logtakes multiple arguments, and will sys.inspectthe first arg if it's not a string.

两者都只是写入标准输出流。不同之处在于sys.putsjusttoString是第一个参数,并console.log接受多个参数,sys.inspect如果第一个参数不是字符串,它将是第一个参数。

回答by angry kiwi

Puts is deprecated in since version 0.2.3

自 0.2.3 版起已弃用 Puts

回答by Rajkamal Subramanian

console.dir(objectToInspect)

This might be another way to inspect objects.

这可能是另一种检查对象的方法。

回答by daralthus

Also you can use console.log without requiring the sys module.

您也可以在不需要 sys 模块的情况下使用 console.log。

回答by Amol M Kulkarni

sys.puts([...]);

Is a synchronous output function. Simply it is a blocking function as in Node.js language.

是同步输出功能。简单地说,它是 Node.js 语言中的阻塞函数。

console.log([data], [...]);

Prints to stdout with newline.

使用换行符打印到标准输出。

For more Info:

更多信息:

http://nodejs.org/api/stdio.html#stdio_console_log_data

http://nodejs.org/api/stdio.html#stdio_console_log_data

http://nodejs.org/api/util.html#util_util_puts

http://nodejs.org/api/util.html#util_util_puts

Note: 'sys' module was renamed to be 'util' (Supported by link) It was a Fix #3577

注意:'sys' 模块被重命名为 'util'(由链接支持)这是一个修复 #3577