JavaScript学习与实践(17)
JS中的转意字符,你可以在一个字符串中加入特定的符号,用反斜杠“\”加上特定的字符,包块省略号,新的行,引号,或者其他的特定符号
看看下面的JS代码
var txt="We are the so-called "Vikings" from the north." document.write(txt)
在JS中字符串是开始和结束于单引号或者双引号,这个意思就是,这个字符串被分为We are the so-called,解决的方法就是在"Viking"引号前面加上反斜杠“\”,
var txt="We are the so-called \"Vikings\" from the north." document.write(txt)
这样脚本就能输出正确的We are the so-called "Vikings" from the north.
下面是另外一个例子,
document.write ("You \& me are singing!")
句子输出
You & me are singing!
下面是其余的一些字符
\'
single quote
\"
double quote
\&
ampersand
\\
backslash
\n
new line
\r
carriage return
\t
tab
\b
backspace
\f
form feed
最后这里还有一些重要的细节需要大家知道
JS是一个语法敏感的语言
一个函数“myfunction”是绝对不和“myFunction”等价的,一个变量“myVar”也同样不和“myvar”等价
所以,你一定要仔细检查你的拼写,包括变量和对象和函数等等
空格:
JS忽略额外的空格,你可以加入一些空格来使你的代码可读性更加好,
下面是等价的写法:
name="Hege" name = "Hege"
你可以把一行字符串代码分写在两行,不过要加上反斜杠来结尾“\”
例如:
document.write("Hello \ World!")
但是不能这样写:
document.write \ ("Hello World!")
注释
你可以在JS中加入注释,用双斜杠来注释当钱行,例如:
//this is a comment document.write("Hello World!")
用/*...*/来注释多行
/* This is a comment block. It contains several lines */ document.write("Hello World!")
到这里,JS的基础部分已经学习完毕,这些只是一些很浅显的东西,但是我学习的历程,
希望大家支持,欢迎大家仍鸡蛋,不过对我自己来说还是有很大帮助的,
希望大家喜欢,谢谢!欢迎留言