JavaScript的字面量和Unicode编码
in Note with 0 comment
JavaScript的字面量和Unicode编码
in Note with 0 comment

字面值

字面值是由语法表达式定义的常量、或是通过由一定字辞组成的语词表达式定义的常量

字面值是常量,其值是固定的,而且在程序脚本运行中不可更改

数组字面值

数组字面值是一个封闭在方括号对([])中的包含有零个或多个表达式的列表,其中每个表达式代表数组的一个元素,数组字面值同时也是数组对象。

var coffees = ["French Roast", "Colombian", "Kona"];

var a=[3];

console.log(a.length); // 1

console.log(a[0]); // 3

多余逗号

var fish = ["Lion", , "Angel"];

这个数组中,有两个已被赋值的元素,和一个空元素(fish[0]是"Lion",fish[1]是undefined,而fish[2]是"Angel";译注:此时数组的长度属性fish.length是3)

若你在元素列表的尾部添加了一个逗号,它会被忽略。

var myList = [ , 'home', , 'school'];

数组的长度是4,元素myList[0]和myList[2]缺失

var myList = ['home', , 'school', , ];

数组的长度是4,元素myList[1]和myList[3]缺失

布尔字面值

布尔类型有两种字面值:true和false

整数

整数可以被表示成十进制(基数为10)、十六进制(基数为16)以及八进制(基数为8)

0, 117 and -345 (decimal, base 10)
015, 0001 and -077 (octal, base 8) 
0x1123, 0x00111 and -0xF1A7 (hexadecimal, "hex" or base 16)

浮点数字面值

语法

[(+|-)][digits][.digits][(E|e)[(+|-)]digits]

例子

3.14      
-.2345789 // -0.23456789
-3.12e+12  // -3.12*10^12
.1e-23    // 0.1*10-23=10-24=1e-24

对象字面值

对象字面值是封闭在花括号对 {} 中的一个对象的零个或多个"属性名-值"对的(元素)列表

var Sales = "Toyota";

function CarTypes(name) {
  return (name === "Honda") ?
    name :
    "Sorry, we don't sell " + name + "." ;
}

var car = { myCar: "Saturn", getCar: CarTypes("Honda"), special: Sales };

console.log(car.myCar);   // Saturn
console.log(car.getCar);  // Honda
console.log(car.special); // Toyota

使用数字或字符串字面值作为属性的名字,或者在另一个字面值内嵌套上一个字面值

var car = { manyCars: {a: "Saab", "b": "Jeep"}, 7: "Mazda" };

console.log(car.manyCars.b); // Jeep
console.log(car[7]); // Mazda

对象属性名字可以是任意字符串,包括空串。如果对象属性名字不是合法的javascript标识符,它必须用 "" 包裹。属性的名字不合法的话,只能通过类数组标记 [] 访问和赋值

var unusualPropertyNames = {
  "": "An empty string",
  "!": "Bang!"
}
console.log(unusualPropertyNames."");   // SyntaxError: Unexpected string
console.log(unusualPropertyNames[""]);  // An empty string
console.log(unusualPropertyNames.!);    // SyntaxError: Unexpected token !
console.log(unusualPropertyNames["!"]); // Bang!

注意

var foo = {a: "alpha", 2: "two"};
console.log(foo.a);    // alpha
console.log(foo[2]);   // two
//console.log(foo.2);  // Error: missing ) after argument list
//console.log(foo[a]); // Error: a is not defined
console.log(foo["a"]); // alpha
console.log(foo["2"]); // two

正则表达式字面值

var re = /ab+c/;

字符串字面值

字符串字面值可以包含有零个或多个字符,由双引号(")对或单引号(‘)对包围,下面的例子都是字符串字面值

"foo"
'bar'
"1234"
"one line \n another line"
"John's cat"

可以在字符串字面值上使用字符串对象的所有方法,如:能用对字符串字面值使用类似String.length的属性

"John's cat".length

使用特殊字符

JavaScript 特殊字符

CharacterMeaning
\0Null Byte
\bBackspace
\fForm feed
\nNew line
\rCarriage return
\tTab
\vVertical tab
\'Apostrophe or single quote
\"Double quote
\Backslash character
\XXXThe character with the Latin-1 encoding specified by up to three octal digits XXX between 0 and 377. For example, \251 is the octal sequence for the copyright symbol.
\xXXThe character with the Latin-1 encoding specified by the two hexadecimal digits XX between 00 and FF. For example, \xA9 is the hexadecimal sequence for the copyright symbol.
\uXXXXThe Unicode character specified by the four hexadecimal digits XXXX. For example, \u00A9 is the Unicode sequence for the copyright symbol.
\u{XXXXX}Unicode code point escapes. For example, \u{2F804} is the same as the simple Unicode escapes \uD87E\uDC04.

转义字符

var quote = "He read \"The Cremation of Sam McGee\" by R.W. Service.";
console.log(quote);

He read "The Cremation of Sam McGee" by R.W. Service.

把文件路径 c:\temp 赋值给一个字符串,如

var home = "c:\\temp";

在换行之前加上反斜线以转义换行

var str = "this string \
is broken \
across multiple\
lines."
console.log(str);   // this string is broken across multiplelines.

也可以以用行末的换行符转义和转义的换行

var poem = 
"Roses are red,\n\
Violets are blue.\n\
I'm schizophrenic,\n\
And so am I."

Unicode编码

Unicode是一种通用字符编码标准,用于世界上主要书面语言的交换和显示。Unicode允许多语言文本的交换、处理和显示,以及通用的技术和数学符号的使用。

兼容于ASCII和ISO标准

Unicode的UTF-8编码与ASCII字符是兼容,并且被许多程序所支持。前128个Unicode字符与ASCII字符一一对应,并具有相同的字节值。从U+0020至U+007E的Unicode字符等价于从0x20到0x7E的ASCII字符。ASCII支持拉丁字母并使用7位字符集,而UTF-8的每个字符占用一到四个8位组(8位组octet,即1个字节或8位),这样可以表示数百万个字符。

Unicode转义序列

你可以在字符串字面值、正则表达式和标识符中使用Unicode转义序列。转义序列由6个ASCII字符构成:\u和一个4位十六进制数字。

var x = "\u00A9 Netscape Communications";

代码返回一个版权符号和字符串“Netscape Communications”。

JavaScript中Unicode转义序列的用法和Java不同:

在JavaScript中,

在Java中,


阅读资料:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Guide

复习方式:实践(代码写一次、跑一次、测试一次),不懂的地方谷歌,阅读和做笔记

底线原则:宁可重写一次,也不复制粘贴

本次复习内容有:字面值、Unicode编码

复习耗时:大概也2小时···我也不知道为什么这么久···

Responses