为什么控制台。日志的行为呢?

时间:2022-12-29 16:55:58

On the node.js interpreter:-

在节点上。js解释器:-

console.log("A newline character is written like \"\\ n \".");
//output is:-
// A newline character is written like "\ n ".

But when you simply enter this in the node.js interpreter:-

但是当你在节点中输入这个的时候。js解释器:-

"A newline character is written like \"\\ n \"."
// it prints out:-
//'A newline character is written like "\\ n ".'

Does anybody now why this happened? Just curious to know more about node.js Thanks in advance for your answer.

有人知道为什么会这样吗?只是想知道更多关于节点的信息。谢谢你的回答。

1 个解决方案

#1


6  

When logging a string, it gets fully parsed and every character gets escaped, and that's ok, it is the expected behavior.

当记录一个字符串时,它会被完全解析,每个字符都会被转义,这是预期的行为。

Nonethless, displaying a string (not logging), the interpreter tries to show it in the simplest possible form. This will also avoid any misunderstanding for the user who's looking at it. So, basically:

尽管如此,解释器试图以最简单的形式显示字符串(而不是日志记录)。这也将避免对查看它的用户产生任何误解。所以,基本上是:

  • Displaying "\"hi\"" will show '"hi"', because you can write a double quote inside a single quote delimited string without escaping it, and it's much easier to read.

    显示“\”\“hi\”将显示“hi”,因为您可以在一个带分隔符的字符串中编写一个双引号,而不必转义它,而且更容易阅读。

  • Displaying '\'hi\'' will show "'hi'", for the same reason.

    由于同样的原因,显示'\'hi\'将显示''嗨'"。

  • Displaying "\"hi\", 'hey'", with both single and double quotes, will force the interpreter to show you the original string (in the same form you created it), because there's no way to display it wothout escaping either the single or the double quotes, so it can only be shown as "\"hi\", 'hey'".

    显示“\”嗨\”,“嘿”,单和双引号,将迫使翻译给你们原始字符串(在相同的形式创建它),因为没有办法显示wothout逃单引号或双引号,所以它只能显示为“\”嗨\”,‘嘿’”。

Try it by yourself:

自己试一试:

var a = "\"hi\", 'hey'";
> "\"hi\", 'hey'"

var b = "\"hi\"";
> '"hi"'

console.log(a + ", " + b);
> "hi", 'hey', "hi"

#1


6  

When logging a string, it gets fully parsed and every character gets escaped, and that's ok, it is the expected behavior.

当记录一个字符串时,它会被完全解析,每个字符都会被转义,这是预期的行为。

Nonethless, displaying a string (not logging), the interpreter tries to show it in the simplest possible form. This will also avoid any misunderstanding for the user who's looking at it. So, basically:

尽管如此,解释器试图以最简单的形式显示字符串(而不是日志记录)。这也将避免对查看它的用户产生任何误解。所以,基本上是:

  • Displaying "\"hi\"" will show '"hi"', because you can write a double quote inside a single quote delimited string without escaping it, and it's much easier to read.

    显示“\”\“hi\”将显示“hi”,因为您可以在一个带分隔符的字符串中编写一个双引号,而不必转义它,而且更容易阅读。

  • Displaying '\'hi\'' will show "'hi'", for the same reason.

    由于同样的原因,显示'\'hi\'将显示''嗨'"。

  • Displaying "\"hi\", 'hey'", with both single and double quotes, will force the interpreter to show you the original string (in the same form you created it), because there's no way to display it wothout escaping either the single or the double quotes, so it can only be shown as "\"hi\", 'hey'".

    显示“\”嗨\”,“嘿”,单和双引号,将迫使翻译给你们原始字符串(在相同的形式创建它),因为没有办法显示wothout逃单引号或双引号,所以它只能显示为“\”嗨\”,‘嘿’”。

Try it by yourself:

自己试一试:

var a = "\"hi\", 'hey'";
> "\"hi\", 'hey'"

var b = "\"hi\"";
> '"hi"'

console.log(a + ", " + b);
> "hi", 'hey', "hi"