本文最后更新于 2025年8月14日 星期四 10:51
红书指:JavaScript 高级程序设计(第 4 版), 2020 年 9 月第 2 版, 第 10
次印刷, 978-7-115-54538-1。
基本引用类型
Date
完整方法见红书 P106。
1 2 3 4 5 6 7 8 9
| let now = new Date(); console.log(now.toDateString()); console.log(now.toTimeString()); console.log(now.toLocaleDateString()); console.log(now.toLocaleTimeString()); console.log(now.toUTCString());
let now = Date.now();
|
RegExp
- g:查找全部内容,而不是找到第一个匹配结束。
- i:忽略大小写。
- m:多行模式。
- y:粘附模式,只从
lastIndex 开始查找。
- u:Unicode 模式。
- s:匹配任何字符,包括
\n或r。
1 2 3
| let text = "some text here"; let pattern = /pattern/gimsuy; let matches = pattern.exec(text);
|
matches 属性:index input。
pattern 属性:lastIndex。
RegExp 属性
(无 Web 标准,不要在生产环境使用)
.input |
.$_ |
|
.lastMatch |
[$&] |
|
.lastParen |
[$+] |
最后匹配的捕获组(非标准特性) |
.leftContext |
[$`] |
|
.rightContext |
[$'] |
|
原始值包装类型
1 2 3 4 5 6 7
| let v = 2; let n = +v; let m = new Number(v);
console.log(n.toFixed(2)); console.log(n.toExponential(2)); console.log(n.toPrecision(2));
|
数据类型
Number
1 2
| parseInt("123", 按几进制解析); parseFloat("123");
|
字符串
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| s.toUpperCase(); s.toLowerCase();
s.indexOf("");
s.substring(0, 3); s.substring(3);
String.raw.raw;
let a = 6; let b = 9; function simpleTag(strings, ...expressions) { console.log(strings); for (const expression of expressions) { console.log(expression); } return "foobar"; } let taggedResult = simpleTag`${a} + ${b} = ${a + b}`;
console.log(taggedResult);
function zipTag(strings, ...expressions) { return ( strings[0] + expressions.map((e, i) => `${e}${strings[i + 1]}`).join("") ); } let taggedResult = zipTag`${a} + ${b} = ${a + b}`; console.log(taggedResult);
|
数组
1 2 3 4 5
| a.indexOf(10);
a.slice(0, 3); a.slice(3); a.slice();
|
push() 向末尾添加,pop() 删除末尾。
unshift() 向头部添加,shift() 删除头部。
1 2 3 4 5 6
| a.splice(1, 3); a.splice(1, 3, ..., 'A', 'B'); a.splice(1, 0, ..., 'A', 'B');
a.concat(1,[2, 3]);
|
join()
1 2
| let arr = ["A", "B", "C", 1, 2, 3]; arr.join("-");
|
排序后输出
1 2
| let arr = ["A", "B", "C", "D"]; console.log(`${arr.sort().slice(0, arr.length).join(",")}`);
|