简体   繁体   English

JavaScript 初学者的引号问题

[英]JavaScript beginner troubles with quotation marks

I'm trying to learn JS from a book (Beginner JavaScript by Jeremy McPeak), but I'm stuck with this code:我正在尝试从一本书(Jeremy McPeak 的初学者 JavaScript)中学习 JS,但我坚持使用以下代码:

<script>
var myString = "56.02 degrees centigrade";

document.write("\"" + myString + "\" is " + parseInt(myString, 10) +
" as an integer" + "<br/>");
</script>

The result in html is this: "56.02 degrees centigrade" is 56 as an integer . html 中的结果是这样的: "56.02 degrees centigrade" is 56 as an integer

What I don't understand is not explained in the book - why is this code written as it is?我不明白的在书中没有解释——为什么这段代码是这样写的? Could someone please explain in layman's terms why do we begin with "\\"" (why not just \\" since that's escape sequence for double quotes), why after that we have to write "\\" (shouldn't it be just \\" if we want closing quotation marks for myString), and why is this written after that: is " ?有人可以用外行的术语解释为什么我们以"\\""开头(为什么不只是\\"因为这是双引号的转义序列),为什么在那之后我们必须写"\\" (不应该只是\\"如果我们想要 myString 的右引号),为什么在这之后写成: is " Basically, that first part really confused me.基本上,第一部分真的让我感到困惑。

In Javascript (and most other languages), you write a string by putting a sequence of characters between a pair of quote characters.在 Javascript(和大多数其他语言)中,您可以通过在一对引号字符之间放置一系列字符来编写字符串。 So a string containing abc is written as所以包含abc的字符串写成

"abc"

If you want one of the characters in the string to be a quote character, you have to escape it, so it won't be treated as the end of the string.如果您希望字符串中的其中一个字符是引号字符,则必须对其进行转义,因此它不会被视为字符串的结尾。 So a string containing abc"def would be written as:所以包含abc"def的字符串将被写成:

"abc\"def"

This is demonstrated in your code where it has这在您的代码中得到了证明

"\" is "

This is a string that begins with a literal quote followed by the word is .这是一个以文字引号开头的字符串,后跟单词is

If you want a string containing only a quote character, you need to put an escaped quote between the quotes that indicate that you're writing a string:如果您想要一个包含引号字符的字符串,则需要在表示您正在编写字符串的引号之间放置一个转义的引号:

"\""

That's what's at the beginning of the concatenation expression in your code.这就是代码中连接表达式开头的内容。

If you just wrote如果你只是写

\"

that would be an escaped quote.那将是一个转义的报价。 But since it's not inside quotes, it's not a string -- it's not valid syntax for anything.但是因为它不在引号内,所以它不是一个字符串——它不是任何东西的有效语法。

In Javascript, there's another option.在 Javascript 中,还有另一种选择。 It allows both single and double quotes to be used to surround a string.它允许使用单引号和双引号将字符串括起来。 So if you have a string containing a double quote, you can put it inside single quotes:所以如果你有一个包含双引号的字符串,你可以把它放在单引号内:

'"'

You don't need to escape it because the double quote doesn't end a string that begins with single quote.您不需要转义它,因为双引号不会结束以单引号开头的字符串。 Conversely, if you want to put a single quote in a string, use double quotes as the delimiters:相反,如果要在字符串中放置单引号,请使用双引号作为分隔符:

"This is Barry's answer"

myString is a string. myString 是一个字符串。 The function parseInt(string, radix) will parse a string as a integer ( see this for more examples ).函数 parseInt(string, radix) 将字符串解析为整数(更多示例请参见此处)。

The quotes are the way they are so that the output has the quotes displayed.引号是它们的方式,以便输出显示引号。 If you don't want the quotes in the output the js could be simplified to:如果您不希望输出中的引号,则 js 可以简化为:

document.write(myString + " is " + parseInt(myString, 10) + " as an integer" + "<br/>");

but this wouldn't be as clear in showing how parseInt works.但这在展示 parseInt 的工作原理时并不清楚。

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

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