简体   繁体   English

JavaScript转义字符串在Firefox开发人员工具上不起作用

[英]JavaScript Escaping Strings don’t work on Firefox Developer Tools

While I was trying the escaping strings on Firefox, I noticed that escaping strings don't work on Firefox Developer Tools , unless they are inside a console.log() or alert() message. 在Firefox上尝试转义字符串时,我注意到转义字符串在Firefox开发人员工具上不起作用,除非它们在console.log()alert()消息中。

For example, if you just type inside the console the following message: 例如,如果您仅在控制台内键入以下消息:

"The man whispered, \"please speak to me.\""

the result will be exactly the same with what you typed: 结果将与您键入的内容完全相同:

"The man whispered, \"please speak to me.\""

However, if you include the message in console.log() , then it works fine: 但是,如果将消息包含在console.log() ,则可以正常工作:

Input: 输入:

console.log("The man whispered, \"please speak to me.\"");

Output: 输出:

The man whispered, "please speak to me."

I don't know if it's some kind of bug or there's something deeper I'm not aware of, so I post it here and I hope someone could elaborate on that. 我不知道这是某种错误还是更深层次的我不知道,所以我将其发布在这里,希望有人能对此进行详细说明。

Firefox's Web Console is pulling double duty as a REPL environment and as the place where window.console dumps its output. Firefox的Web控制台作为REPL环境window.console转储其输出的地方,承担着双重责任。 When you type '"' in the Web Console, it reads and evaluates that string as a line of Javascript, and gives you the return value in a form it thinks you can use. Since '"' evaluates to a string, Firefox gives you that result as a string literal, "\\"" . But it still only contains one character, U+0022 QUOTATION MARK. You can see this for yourself with further testing: 当您在Web控制台中键入'"'时,它将读取并评估该字符串作为Java语言行,并以您认为可以使用的形式为您提供返回值。由于'"'为字符串,因此Firefox会为您提供结果为字符串文字"\\"" ,但是它仍然只包含一个字符U + 0022 QUOTATION MARK。您可以通过进一步的测试亲自看到以下内容:

» var str = '"'
⬅ undefined
» str
⬅ "\""
» str.length
⬅ 1
» str.charCodeAt(0)
⬅ 34

When you use console.log() , on the other hand, the Web Console is dumping the objects given to console.log as log messages. 另一方面,当您使用console.log() ,Web控制台将转储给console.log的对象作为日志消息转储。 Firefox has a different idea of what makes a useful log message: for strings, an easy-to-read message is better than a string literal you could copy back into your code. Firefox对于产生有用的日志消息有不同的想法:对于字符串,易于阅读的消息比可以复制回代码中的字符串文字更好。 Thus, 从而,

» console.log(str)
  "                                     debugger eval code:1:1
⬅ undefined

Note the undefined at the bottom: the Console is still REPLing. 请注意底部的undefined :控制台仍在替换。 The " line is console.log intruding on your conversation. "行是console.log介入您的对话。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM