JavaScript 初学者的引号问题 [英] JavaScript beginner troubles with quotation marks

查看:20
本文介绍了JavaScript 初学者的引号问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从一本书中学习 JS(Jeremy McPeak 的初学者 JavaScript),但我被这段代码困住了:

I'm trying to learn JS from a book (Beginner JavaScript by Jeremy McPeak), but I'm stuck with this code:

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

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

html 中的结果是这样的:56.02 摄氏度"是 56 作为一个整数.

The result in html is this: "56.02 degrees centigrade" is 56 as an integer.

我不明白的书里没有解释——为什么这段代码是这样写的?有人可以用外行的术语解释为什么我们以 "\"" 开头(为什么不只是 \" 因为这是双引号的转义序列),为什么在那之后我们有写 "\" (如果我们想要为 myString 加上右引号,不应该只是 \" ),为什么在这之后写:is?基本上,第一部分真的让我很困惑.

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 "? Basically, that first part really confused me.

推荐答案

在 Javascript(和大多数其他语言)中,您可以通过在一对引号字符之间放置一系列字符来编写字符串.所以包含 abc 的字符串写成

In Javascript (and most other languages), you write a string by putting a sequence of characters between a pair of quote characters. So a string containing abc is written as

"abc"

如果您希望字符串中的某个字符成为引号字符,则必须对其进行转义,因此它不会被视为字符串的结尾.所以包含 abc"def 的字符串将被写成:

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"

这在您的代码中展示了

"\" is "

这是一个以文字引号开头的字符串,后跟单词 is.

This is a string that begins with a literal quote followed by the word 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.

如果你刚刚写了

\"

那将是一个转义的引用.但是因为它不在引号内,所以它不是一个字符串——它不是任何东西的有效语法.

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.

在 Javascript 中,还有另一种选择.它允许使用单引号和双引号将字符串括起来.所以如果你有一个包含双引号的字符串,你可以把它放在单引号内:

In Javascript, there's another option. 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"

这篇关于JavaScript 初学者的引号问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆