Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
1755 commits
Select commit Hold shift + click to select a range
a6e8a40
refactor: add tencent.txt
ruanyf Feb 23, 2020
b0b10a3
refactor: add tencent.txt
ruanyf Feb 23, 2020
717f4e4
docs: fix #952
ruanyf Feb 23, 2020
296159a
修正一处小地方
EggOxygen Feb 25, 2020
57701fc
docs(class): fix typo
ruanyf Feb 27, 2020
2e1c107
Merge pull request #954 from EggOxygen/patch-1
ruanyf Feb 27, 2020
b4ec056
fix: 使语句更通顺
anhongpeng Feb 29, 2020
518f311
refactor: update disqus url to HTTPS
ruanyf Mar 3, 2020
6800796
docs(async): 如何处理数组的异步操作
ruanyf Mar 3, 2020
3405f13
Merge pull request #955 from AnHongpeng/gh-pages
ruanyf Mar 4, 2020
6069e3f
使用core-js替代过时的@babel/polyfill
Mookiepiece Mar 7, 2020
130cdee
Merge pull request #958 from Mookiepiece/master
ruanyf Mar 10, 2020
428a748
docs(intro): delete Babel API #958
ruanyf Mar 10, 2020
0f65697
perf: remove unnecessary closure
MingCheHarden Mar 13, 2020
21e3cfb
Merge pull request #960 from yaoguoli/patch-1
ruanyf Mar 16, 2020
e692306
fix: arraybuffer.md add `new`
hou-pu Mar 18, 2020
74acd64
Merge pull request #961 from wangzongxu/patch-1
ruanyf Mar 18, 2020
f18bd46
修复表述错误
linmii Mar 21, 2020
2061ad9
修正 `exec()` 拼写
kidonng Mar 22, 2020
f330662
Merge pull request #964 from kidonng/patch-1
ruanyf Mar 23, 2020
f71a2ce
docs(number): fix #965
ruanyf Mar 23, 2020
491f6f0
Merge pull request #962 from linmii/patch-1
ruanyf Mar 23, 2020
cd0310c
docs(object): 修改扩展运算符自动执行取值函数 get 的例子
ruanyf Mar 23, 2020
31ab7b1
去除多余文字
linmii Mar 24, 2020
fe72377
refactor: edit sponsor banner
ruanyf Mar 24, 2020
0068aa5
refactor: edit sponsor banner
ruanyf Mar 25, 2020
f0b8e22
Merge pull request #967 from linmii/patch-2
ruanyf Mar 26, 2020
8b4db43
docs: edit object-methods
ruanyf Mar 30, 2020
7332021
统一“抽象操作的运行流程”的描述
baooab Mar 31, 2020
09d2e0b
Merge pull request #970 from baooab/patch-5
ruanyf Apr 1, 2020
03c9e34
docs(module): update es2020
ruanyf Apr 3, 2020
0849a77
Merge branch 'gh-pages' of github.com:ruanyf/es6tutorial into gh-pages
ruanyf Apr 3, 2020
ac50325
docs: edit Promise
ruanyf Apr 6, 2020
1474f82
fix(proposals): shell exec style
waitingsong Apr 7, 2020
7076dd5
docs(math): fix #973
ruanyf Apr 9, 2020
38963f7
Merge pull request #971 from waitingsong/shell-exec
ruanyf Apr 9, 2020
c3dd90d
refactor: edit ditto/support
ruanyf Apr 13, 2020
f810124
Merge branch 'gh-pages' of github.com:ruanyf/es6tutorial into gh-pages
ruanyf Apr 13, 2020
e11dc14
Update module.md
suzhongping Apr 24, 2020
d65a547
Merge pull request #979 from suzhongping/patch-1
ruanyf Apr 25, 2020
2f908a5
完善Reflect.ownkeys()方法的描述
chenweitech May 8, 2020
6f59bc3
docs(proxy): edit defineProperty
ruanyf May 13, 2020
3832b6a
refactor: adjust support date
ruanyf May 13, 2020
18e372d
Merge pull request #982 from chenweitech/patch-1
ruanyf May 14, 2020
55cbbb8
fix: li tag click bug #984
ruanyf May 14, 2020
323e962
docs(set): fix #991
ruanyf May 31, 2020
f0a92d5
docs(regex): fixed matchall() #992
ruanyf Jun 12, 2020
0219f20
docs(object): edit 链判断运算符 ?.
ruanyf Jun 21, 2020
d59b301
docs(number): edit BigInt
ruanyf Jul 6, 2020
4425866
docs(object): edit null 判断运算符
ruanyf Jul 7, 2020
064ee65
docs(object-methods): edit Object.assign()
ruanyf Jul 7, 2020
e785137
docs(let): fix #1001
ruanyf Aug 6, 2020
9461a92
docs: edit README
ruanyf Aug 14, 2020
266199c
docs(module-loader): edit Node.js 的模块加载
ruanyf Aug 19, 2020
09accf9
docs(module-loader): fix typo
ruanyf Aug 19, 2020
7b8c063
docs(module-loader): edit 同时支持两种格式的模块
ruanyf Aug 20, 2020
a841acd
docs(module-loader): fix typo
ruanyf Aug 20, 2020
2613635
docs(module-loader): fix typo
ruanyf Aug 20, 2020
dfe3373
feat: add change theme
Aug 21, 2020
e33a668
upt: delete note
Aug 25, 2020
adb4964
docs(module-loader): fix typo
ruanyf Aug 29, 2020
dd892f8
Merge pull request #1002 from zhidaoliu/gh-pages
ruanyf Aug 29, 2020
4e14b40
docs(object): fix #1004
ruanyf Sep 1, 2020
2f76a9f
(docs): fix typo
hehe1111 Sep 9, 2020
76fc255
docs(promise): edit Promise.reject()
ruanyf Sep 10, 2020
04ff592
Merge pull request #1006 from hehe1111/patch-1
ruanyf Sep 10, 2020
d4952d6
docs(module-loader): fix typo
ruanyf Sep 10, 2020
50b25c1
Merge branch 'gh-pages' of github.com:ruanyf/es6tutorial into gh-pages
ruanyf Sep 10, 2020
6dc250f
docs(decorator): 修改函数提升时代码实际执行顺序
careteenL Sep 12, 2020
c432253
优化了 async 函数内部报错会导致 Promise 对象变为`reject`状态的例子
jacty Sep 12, 2020
f81d202
docs(proxy): fix constrct()
ruanyf Sep 15, 2020
0f1acd3
docs(iterable): fix typo
ruanyf Sep 15, 2020
841eebb
Merge pull request #1007 from careteenL/patch-1
ruanyf Sep 15, 2020
8a5b570
Merge pull request #1008 from jacty/patch-1
ruanyf Sep 15, 2020
10defdb
docs(async): fix typo
ruanyf Sep 15, 2020
9f1f925
Update proxy.md
chua1989 Sep 15, 2020
48e318b
Update proxy.md
chua1989 Sep 15, 2020
c025fc9
Merge pull request #1009 from chua1989/gh-pages
ruanyf Sep 16, 2020
746a918
docs(proxy): edit construct()
ruanyf Sep 16, 2020
6ff7cb8
docs(class): fix private property
ruanyf Sep 17, 2020
c2d1792
docs(set-map): edit WeakMap
ruanyf Sep 22, 2020
4bd4d77
docs(function/arrow fucntion): edit #150
ruanyf Oct 2, 2020
2e082a3
docs(module): fix module suffix
ruanyf Oct 23, 2020
8f852c0
docs(Atomics): Atomics.wake() rename to Atomics.notify()
ruanyf Oct 24, 2020
d1f96fe
docs(proxy): fix this #1014
ruanyf Oct 24, 2020
356b0db
docs(string): add replaceAll()
ruanyf Oct 25, 2020
492fb25
docs(string-methods): add replaceAll()
ruanyf Oct 27, 2020
2ed3a6b
docs(promise): edit Promise.any()
ruanyf Oct 31, 2020
04dddb2
docs: fix wangdoc-es6 #4
ruanyf Oct 31, 2020
861dd41
docs(arraybuffer): edit Atomics.load()
ruanyf Nov 6, 2020
f817164
docs(string-methods): edit replaceAll(), fixed #1018
ruanyf Nov 17, 2020
a326217
docs(string): fix typo
ruanyf Nov 17, 2020
ff47784
docs(style): fixed #1020
ruanyf Nov 24, 2020
584d5d9
docs(generator): edit style
ruanyf Nov 24, 2020
201eebe
docs(async): fixed #1023
ruanyf Nov 29, 2020
bc0ba76
docs(async): edit reduce()
ruanyf Nov 29, 2020
038a080
refactor: edit support banner
ruanyf Dec 2, 2020
761d107
refactor: edit support banner
ruanyf Dec 2, 2020
56d15b3
refactor: edit support banner
ruanyf Dec 3, 2020
d041893
docs(class): edit class intro
ruanyf Dec 9, 2020
cc2420e
docs(function): edit arrow function
ruanyf Dec 17, 2020
1c99572
docs(promise): fix #1028
ruanyf Dec 22, 2020
1d556be
Update promise.md
Geekhyt Dec 22, 2020
78972be
Merge pull request #1029 from Geekhyt/patch-2
ruanyf Dec 22, 2020
4309e6a
docs(regex): edit regex
ruanyf Jan 6, 2021
802ee5e
Merge branch 'gh-pages' of github.com:ruanyf/es6tutorial into gh-pages
ruanyf Jan 6, 2021
b45161a
docs(proxy): fix #1032
ruanyf Jan 22, 2021
9461630
refactor: edit support js
ruanyf Feb 5, 2021
543678f
docs(array): fix #1037
ruanyf Feb 11, 2021
ec9db66
refactor: edit support banner
ruanyf Feb 20, 2021
631a66e
refactor: edit support banner
ruanyf Feb 20, 2021
3929f4f
refactor: edit support banner
ruanyf Feb 23, 2021
e9fd28b
fix: remove 'else'
roc-an Apr 18, 2021
d09058f
fix: 为输出补充示例
roc-an Apr 20, 2021
d39c55d
Merge pull request #1045 from roc-an/patch-2
ruanyf Apr 23, 2021
95f57f8
Merge pull request #1044 from roc-an/patch-1
ruanyf Apr 23, 2021
fa03d29
fix: 强迫症犯了想去掉多余空格好看一些
roc-an Apr 24, 2021
59146b4
docs(proxy): fix set return value #1047
ruanyf Apr 26, 2021
0163114
Merge pull request #1048 from roc-an/patch-3
ruanyf Apr 26, 2021
85b8174
docs(set-map): 改进 WeakSet 和 WeakMap 文档
codeudan May 8, 2021
71af30d
docs(function): fixed 箭头函数的this #1050
ruanyf May 11, 2021
e6b0210
Merge pull request #1052 from codeudan/gh-pages
ruanyf May 12, 2021
ee1b51e
docs(set-map): edit text
ruanyf May 12, 2021
d41bf83
docs(function): 调整不合适的函数描述
codeudan May 14, 2021
53cb4d3
docs: 修正已知的 this 术语错误
codeudan May 15, 2021
4f2353a
Merge pull request #1054 from codeudan/gh-pages
ruanyf May 18, 2021
0503136
Merge pull request #1055 from codeudan/fix/this
ruanyf May 18, 2021
89370af
docs(class): 添加 in 运算符
ruanyf May 24, 2021
8e236cb
refactor: edit support banner
ruanyf Jun 2, 2021
fdac6cc
docs: add ES2021
ruanyf Jul 10, 2021
9181f78
refactor: update chapters
ruanyf Jul 10, 2021
145e840
Update async.md
yonghui-wang Jul 14, 2021
2139df3
Merge pull request #1063 from yonghui-wang/patch-2
ruanyf Jul 14, 2021
6a972df
docs(operator): fix WeakRef typo
ruanyf Jul 14, 2021
9d51292
Update let.md
wgm7512 Jul 22, 2021
864c2c0
docs(proposals): JSON 模块
ruanyf Jul 22, 2021
12ce67f
Merge pull request #1064 from ThroughTheNight/patch-1
ruanyf Jul 22, 2021
4ead6a0
docs(class): fix typo #1065
ruanyf Jul 27, 2021
1f57596
一处笔误
MrCare Jul 31, 2021
5eb76ed
Merge pull request #1066 from MrCare/patch-1
ruanyf Aug 1, 2021
9cf2cca
refactor: edit support banner
ruanyf Aug 4, 2021
562c688
Update function.md
wgm7512 Aug 6, 2021
0549983
fix: BigInt 不是构造函数
roc-an Aug 6, 2021
a802e37
Update operator.md
newbeman Aug 7, 2021
de8fad9
docs(promise): edit Promise.allSettled()
ruanyf Aug 9, 2021
25f95e0
Merge pull request #1071 from ThroughTheNight/patch-2
ruanyf Aug 9, 2021
f56ca0a
Merge pull request #1072 from roc-an/patch-4
ruanyf Aug 9, 2021
4d9b22c
Merge pull request #1073 from newbeman/patch-1
ruanyf Aug 9, 2021
47dec55
docs(class): 加入 static block
ruanyf Sep 2, 2021
d50868c
refactor: edit support banner
ruanyf Sep 2, 2021
84dba56
docs(style): fixed #1076
ruanyf Sep 8, 2021
248b51a
fix typo: 或 -> 会
imba-tjd Sep 8, 2021
daa455f
use https for some links
imba-tjd Sep 9, 2021
c43e5ca
Merge pull request #1077 from imba-tjd/patch-1
ruanyf Sep 9, 2021
9b210c0
Merge pull request #1078 from imba-tjd/patch-2
ruanyf Sep 9, 2021
9388b33
Update intro.md
Lohoyo Sep 17, 2021
a687c39
Merge pull request #1081 from Lohoyo/patch-1
ruanyf Sep 17, 2021
1fdd34e
docs(object): add AggregateError
ruanyf Sep 19, 2021
e17a581
docs(object): edit AggregateError
ruanyf Sep 19, 2021
180c700
docs(style): edit module style suggestion
ruanyf Oct 4, 2021
de76869
docs(string): fixed #1086
ruanyf Oct 19, 2021
5b57360
Update function.md
Lohoyo Oct 19, 2021
cee42e6
Merge pull request #1088 from Lohoyo/patch-2
ruanyf Oct 21, 2021
728b545
Add script suffix to prevent error
vinzid Oct 31, 2021
a82ed10
Merge pull request #1089 from vinzid/patch-1
ruanyf Nov 1, 2021
1a0eaa5
docs(iterator): fixed #1093
ruanyf Nov 13, 2021
79e871c
docs(array): add at()
ruanyf Nov 14, 2021
2215773
Update string-methods.md
ConradZhangcong Nov 16, 2021
c039b6f
Merge pull request #1094 from ConradZhangcong/gh-pages
ruanyf Nov 22, 2021
74de293
Update array.md
ConradZhangcong Nov 23, 2021
f4030e1
Merge pull request #1097 from ConradZhangcong/gh-pages
ruanyf Nov 23, 2021
a066b9a
docs(module-loader): fix typo
ruanyf Nov 23, 2021
a0265ef
refactor: adjust support code
ruanyf Nov 30, 2021
4781407
refactor: adjust support code
ruanyf Nov 30, 2021
664f685
docs(class): fixed #1099
ruanyf Dec 2, 2021
fd3eb89
docs(proxy): fixed #1100
ruanyf Dec 2, 2021
d9ec0db
refact: edit support script
ruanyf Dec 29, 2021
cddf4b8
refactor: edit support banner
ruanyf Dec 31, 2021
255eedb
docs(reg): edit Unicode 属性类
ruanyf Jan 6, 2022
7fa7ee1
docs(class-extends): edit text
ruanyf Jan 24, 2022
e064886
Update class-extends.md
liuxuan1-1 Feb 5, 2022
b270280
Merge pull request #1108 from liuxuan1-1/patch-2
ruanyf Feb 5, 2022
b2a122e
docs(symbol): fixed #1107
ruanyf Feb 6, 2022
bcfe5ee
refactor: add support banner
ruanyf Feb 7, 2022
8d81fef
refactor: add support banner
ruanyf Feb 7, 2022
9218ba8
refactor: add support banner
ruanyf Feb 8, 2022
ce7fd81
Update object.md
5unnyWind Mar 2, 2022
1fad026
Merge pull request #1110 from 5unnyWind/patch-1
ruanyf Mar 8, 2022
8a214a6
docs(array): edit Array.from()
ruanyf Mar 31, 2022
e856f9e
docs(function): edit 函数参数的解构赋值
ruanyf Apr 9, 2022
95e2e79
docs(object): edit 对象的扩展运算符
ruanyf Apr 9, 2022
ef70334
docs(module): edit import()
ruanyf May 29, 2022
8b9f192
docs(promise): edit Promise.any()
Mookiepiece Jun 4, 2022
b5fa350
Merge pull request #1118 from Mookiepiece/gh-pages
ruanyf Jun 7, 2022
e0772de
docs(promise): edit AggregateError
ruanyf Jun 7, 2022
a837d8f
docs: update ES2022
ruanyf Jun 25, 2022
6880b36
docs(class): fix typo
ruanyf Jun 25, 2022
41970f7
docs: update latest proposals
ruanyf Jun 26, 2022
fbd7faf
docs(decorator): add stage 3 proposal content
ruanyf Jun 27, 2022
f6bb1b9
docs(decorator): edit editor notes
ruanyf Jun 27, 2022
f45f032
docs(regex): fix es2022 indices
xt1995726 Jun 30, 2022
d4d888e
typo: match -> match()
xt1995726 Jun 30, 2022
a090a27
docs(async): fix typo
condorheroblog Jul 1, 2022
aab3314
Merge pull request #1121 from xt1995726/gh-pages
ruanyf Jul 6, 2022
a0c47d5
docs(regex): edit d 修饰符
ruanyf Jul 6, 2022
7d22df8
Merge pull request #1122 from condorheroblog/patch-1
ruanyf Jul 6, 2022
7d030c6
docs(async): edit top-level await
ruanyf Jul 6, 2022
4c0201c
docs(class): fixed #1125 private class field
ruanyf Jul 13, 2022
9348fd2
refactor: edit support banner
ruanyf Jul 23, 2022
010de73
docs(set-map): fix typo
nicong622 Jul 24, 2022
829920e
Merge pull request #1128 from nicong622/patch-1
ruanyf Jul 28, 2022
6bf9155
refactor: edit support banner
ruanyf Jul 31, 2022
686fcff
refactor: edit support banner
ruanyf Jul 31, 2022
21edbe4
docs(symbol): edit text
ruanyf Aug 8, 2022
2b448b6
docs(clss-extends): add inheritance of private property
ruanyf Aug 13, 2022
cf4c947
docs(proposals): edit links
ruanyf Aug 31, 2022
eba1ada
docs(proposals): edit Math.signbit()
ruanyf Aug 31, 2022
5730890
fix: line feed causes HTML rendering error view
condorheroblog Sep 1, 2022
6288fc1
Merge pull request #1135 from condorheroblog/patch-2
ruanyf Sep 1, 2022
e808d04
Update class-extends.md
w-l-l Sep 18, 2022
cded3bc
Merge pull request #1137 from w-l-l/patch-1
ruanyf Sep 18, 2022
1a8f46c
docs(class): fix typo
w-l-l Sep 20, 2022
0eaf2d8
Merge pull request #1140 from w-l-l/patch-3
ruanyf Sep 21, 2022
36b55b9
docs(class): 静态块
ruanyf Sep 21, 2022
ef5e3a2
docs(class): fix typo
ruanyf Oct 18, 2022
7f1acfe
docs(module): move import.meta from proposals to module
ruanyf Oct 23, 2022
406ba6d
docs(module): fix import.meta typo
ruanyf Oct 23, 2022
ef2f878
docs(decorator): edit text
ruanyf Oct 23, 2022
add0bba
docs(decorator): fix typo
w-l-l Dec 4, 2022
1623fdc
Merge pull request #1144 from w-l-l/patch-4
ruanyf Dec 7, 2022
90c0ea7
docs(class-extends): add new materials into super()
ruanyf Dec 23, 2022
f9ef8fd
refactor: add wwads banner
ruanyf Jan 10, 2023
c1a81dc
refactor: adjust wwads banner
ruanyf Jan 10, 2023
536ac22
refactor: adjust wwads banner
ruanyf Jan 10, 2023
010daf6
refactor: adjust wwads banner
ruanyf Jan 10, 2023
09a4657
refactor: adjust wwads banner
ruanyf Jan 10, 2023
b9c8b3d
refactor: adjust wwads banner
ruanyf Jan 10, 2023
b638fb0
refactor: adjust wwads banner
ruanyf Jan 10, 2023
9007f1f
refactor: prism添加对typescript的支持
likeke1997 Jan 16, 2023
6d244f6
Merge pull request #1145 from likeke1997/gh-pages
ruanyf Jan 19, 2023
7fc248d
Update arraybuffer.md
peterwangfeng Feb 1, 2023
07e9c15
Merge pull request #1146 from peterwangfeng/patch-1
ruanyf Feb 2, 2023
7523f55
docs(arraybuffer): update DataView methods
ruanyf Feb 2, 2023
60e128c
module-loader: remove `--experimental-conditional-exports`
islishude Feb 12, 2023
f7b84f2
Merge pull request #1148 from islishude/patch-1
ruanyf Feb 12, 2023
b20e38b
docs(set): fix delete fn
Hencky Mar 2, 2023
badabbf
Merge pull request #1149 from Hencky/gh-pages
ruanyf Mar 2, 2023
0928b9c
docs(set-map): WeakSet methods
Hencky Mar 6, 2023
b68c36d
Merge pull request #1150 from Hencky/gh-pages
ruanyf Mar 6, 2023
50e9ced
docs(operator): #! 命令
ruanyf Apr 7, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
docs: add ES2021
  • Loading branch information
ruanyf committed Jul 10, 2021
commit fdac6ccb2e93dc4956e8b8ab10cdfaa912b4ca73
124 changes: 92 additions & 32 deletions docs/number.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,97 @@ Number('0b111') // 7
Number('0o10') // 8
```

## 数值分隔符

欧美语言中,较长的数值允许每三位添加一个分隔符(通常是一个逗号),增加数值的可读性。比如,`1000`可以写作`1,000`。

[ES2021](https://github.com/tc39/proposal-numeric-separator),允许 JavaScript 的数值使用下划线(`_`)作为分隔符。

```javascript
let budget = 1_000_000_000_000;
budget === 10 ** 12 // true
```

这个数值分隔符没有指定间隔的位数,也就是说,可以每三位添加一个分隔符,也可以每一位、每两位、每四位添加一个。

```javascript
123_00 === 12_300 // true

12345_00 === 123_4500 // true
12345_00 === 1_234_500 // true
```

小数和科学计数法也可以使用数值分隔符。

```javascript
// 小数
0.000_001

// 科学计数法
1e10_000
```

数值分隔符有几个使用注意点。

- 不能放在数值的最前面(leading)或最后面(trailing)。
- 不能两个或两个以上的分隔符连在一起。
- 小数点的前后不能有分隔符。
- 科学计数法里面,表示指数的`e`或`E`前后不能有分隔符。

下面的写法都会报错。

```javascript
// 全部报错
3_.141
3._141
1_e12
1e_12
123__456
_1464301
1464301_
```

除了十进制,其他进制的数值也可以使用分隔符。

```javascript
// 二进制
0b1010_0001_1000_0101
// 十六进制
0xA0_B0_C0
```

可以看到,数值分隔符可以按字节顺序分隔数值,这在操作二进制位时,非常有用。

注意,分隔符不能紧跟着进制的前缀`0b`、`0B`、`0o`、`0O`、`0x`、`0X`。

```javascript
// 报错
0_b111111000
0b_111111000
```

数值分隔符只是一种书写便利,对于 JavaScript 内部数值的存储和输出,并没有影响。

```javascript
let num = 12_345;

num // 12345
num.toString() // 12345
```

上面示例中,变量`num`的值为`12_345`,但是内部存储和输出的时候,都不会有数值分隔符。

下面三个将字符串转成数值的函数,不支持数值分隔符。主要原因是语言的设计者认为,数值分隔符主要是为了编码时书写数值的方便,而不是为了处理外部输入的数据。

- Number()
- parseInt()
- parseFloat()

```javascript
Number('123_456') // NaN
parseInt('123_456') // 123
```

## Number.isFinite(), Number.isNaN()

ES6 在`Number`对象上,新提供了`Number.isFinite()`和`Number.isNaN()`两个方法。
Expand Down Expand Up @@ -655,42 +746,11 @@ ES6 新增了 6 个双曲函数方法。
- `Math.acosh(x)` 返回`x`的反双曲余弦(inverse hyperbolic cosine)
- `Math.atanh(x)` 返回`x`的反双曲正切(inverse hyperbolic tangent)

## 指数运算符

ES2016 新增了一个指数运算符(`**`)。

```javascript
2 ** 2 // 4
2 ** 3 // 8
```

这个运算符的一个特点是右结合,而不是常见的左结合。多个指数运算符连用时,是从最右边开始计算的。

```javascript
// 相当于 2 ** (3 ** 2)
2 ** 3 ** 2
// 512
```

上面代码中,首先计算的是第二个指数运算符,而不是第一个。

指数运算符可以与等号结合,形成一个新的赋值运算符(`**=`)。

```javascript
let a = 1.5;
a **= 2;
// 等同于 a = a * a;

let b = 4;
b **= 3;
// 等同于 b = b * b * b;
```

## BigInt 数据类型

### 简介

JavaScript 所有数字都保存成 64 位浮点数,这给数值的表示带来了两大限制。一是数值的精度只能到 53 个二进制位(相当于 16 个十进制位),大于这个范围的整数,JavaScript 是无法精确表示的,这使得 JavaScript 不适合进行科学和金融方面的精确计算。二是大于或等于2的1024次方的数值,JavaScript 无法表示,会返回`Infinity`。
JavaScript 所有数字都保存成 64 位浮点数,这给数值的表示带来了两大限制。一是数值的精度只能到 53 个二进制位(相当于 16 个十进制位),大于这个范围的整数,JavaScript 是无法精确表示,这使得 JavaScript 不适合进行科学和金融方面的精确计算。二是大于或等于2的1024次方的数值,JavaScript 无法表示,会返回`Infinity`。

```javascript
// 超过 53 个二进制位的数值,无法保持精度
Expand Down
229 changes: 0 additions & 229 deletions docs/object.md
Original file line number Diff line number Diff line change
Expand Up @@ -704,232 +704,3 @@ let aWithXGetter = { ...a }; // 报错

上面例子中,取值函数`get`在扩展`a`对象时会自动执行,导致报错。

## 链判断运算符

编程实务中,如果读取对象内部的某个属性,往往需要判断一下该对象是否存在。比如,要读取`message.body.user.firstName`,安全的写法是写成下面这样。

```javascript
// 错误的写法
const firstName = message.body.user.firstName;

// 正确的写法
const firstName = (message
&& message.body
&& message.body.user
&& message.body.user.firstName) || 'default';
```

上面例子中,`firstName`属性在对象的第四层,所以需要判断四次,每一层是否有值。

三元运算符`?:`也常用于判断对象是否存在。

```javascript
const fooInput = myForm.querySelector('input[name=foo]')
const fooValue = fooInput ? fooInput.value : undefined
```

上面例子中,必须先判断`fooInput`是否存在,才能读取`fooInput.value`。

这样的层层判断非常麻烦,因此 [ES2020](https://github.com/tc39/proposal-optional-chaining) 引入了“链判断运算符”(optional chaining operator)`?.`,简化上面的写法。

```javascript
const firstName = message?.body?.user?.firstName || 'default';
const fooValue = myForm.querySelector('input[name=foo]')?.value
```

上面代码使用了`?.`运算符,直接在链式调用的时候判断,左侧的对象是否为`null`或`undefined`。如果是的,就不再往下运算,而是返回`undefined`。

下面是判断对象方法是否存在,如果存在就立即执行的例子。

```javascript
iterator.return?.()
```

上面代码中,`iterator.return`如果有定义,就会调用该方法,否则`iterator.return`直接返回`undefined`,不再执行`?.`后面的部分。

对于那些可能没有实现的方法,这个运算符尤其有用。

```javascript
if (myForm.checkValidity?.() === false) {
// 表单校验失败
return;
}
```

上面代码中,老式浏览器的表单可能没有`checkValidity`这个方法,这时`?.`运算符就会返回`undefined`,判断语句就变成了`undefined === false`,所以就会跳过下面的代码。

链判断运算符有三种用法。

- `obj?.prop` // 对象属性
- `obj?.[expr]` // 同上
- `func?.(...args)` // 函数或对象方法的调用

下面是`obj?.[expr]`用法的一个例子。

```bash
let hex = "#C0FFEE".match(/#([A-Z]+)/i)?.[1];
```

上面例子中,字符串的`match()`方法,如果没有发现匹配会返回`null`,如果发现匹配会返回一个数组,`?.`运算符起到了判断作用。

下面是`?.`运算符常见形式,以及不使用该运算符时的等价形式。

```javascript
a?.b
// 等同于
a == null ? undefined : a.b

a?.[x]
// 等同于
a == null ? undefined : a[x]

a?.b()
// 等同于
a == null ? undefined : a.b()

a?.()
// 等同于
a == null ? undefined : a()
```

上面代码中,特别注意后两种形式,如果`a?.b()`里面的`a.b`不是函数,不可调用,那么`a?.b()`是会报错的。`a?.()`也是如此,如果`a`不是`null`或`undefined`,但也不是函数,那么`a?.()`会报错。

使用这个运算符,有几个注意点。

(1)短路机制

`?.`运算符相当于一种短路机制,只要不满足条件,就不再往下执行。

```javascript
a?.[++x]
// 等同于
a == null ? undefined : a[++x]
```

上面代码中,如果`a`是`undefined`或`null`,那么`x`不会进行递增运算。也就是说,链判断运算符一旦为真,右侧的表达式就不再求值。

(2)delete 运算符

```javascript
delete a?.b
// 等同于
a == null ? undefined : delete a.b
```

上面代码中,如果`a`是`undefined`或`null`,会直接返回`undefined`,而不会进行`delete`运算。

(3)括号的影响

如果属性链有圆括号,链判断运算符对圆括号外部没有影响,只对圆括号内部有影响。

```javascript
(a?.b).c
// 等价于
(a == null ? undefined : a.b).c
```

上面代码中,`?.`对圆括号外部没有影响,不管`a`对象是否存在,圆括号后面的`.c`总是会执行。

一般来说,使用`?.`运算符的场合,不应该使用圆括号。

(4)报错场合

以下写法是禁止的,会报错。

```javascript
// 构造函数
new a?.()
new a?.b()

// 链判断运算符的右侧有模板字符串
a?.`{b}`
a?.b`{c}`

// 链判断运算符的左侧是 super
super?.()
super?.foo

// 链运算符用于赋值运算符左侧
a?.b = c
```

(5)右侧不得为十进制数值

为了保证兼容以前的代码,允许`foo?.3:0`被解析成`foo ? .3 : 0`,因此规定如果`?.`后面紧跟一个十进制数字,那么`?.`不再被看成是一个完整的运算符,而会按照三元运算符进行处理,也就是说,那个小数点会归属于后面的十进制数字,形成一个小数。

## Null 判断运算符

读取对象属性的时候,如果某个属性的值是`null`或`undefined`,有时候需要为它们指定默认值。常见做法是通过`||`运算符指定默认值。

```javascript
const headerText = response.settings.headerText || 'Hello, world!';
const animationDuration = response.settings.animationDuration || 300;
const showSplashScreen = response.settings.showSplashScreen || true;
```

上面的三行代码都通过`||`运算符指定默认值,但是这样写是错的。开发者的原意是,只要属性的值为`null`或`undefined`,默认值就会生效,但是属性的值如果为空字符串或`false`或`0`,默认值也会生效。

为了避免这种情况,[ES2020](https://github.com/tc39/proposal-nullish-coalescing) 引入了一个新的 Null 判断运算符`??`。它的行为类似`||`,但是只有运算符左侧的值为`null`或`undefined`时,才会返回右侧的值。

```javascript
const headerText = response.settings.headerText ?? 'Hello, world!';
const animationDuration = response.settings.animationDuration ?? 300;
const showSplashScreen = response.settings.showSplashScreen ?? true;
```

上面代码中,默认值只有在左侧属性值为`null`或`undefined`时,才会生效。

这个运算符的一个目的,就是跟链判断运算符`?.`配合使用,为`null`或`undefined`的值设置默认值。

```javascript
const animationDuration = response.settings?.animationDuration ?? 300;
```

上面代码中,如果`response.settings`是`null`或`undefined`,或者`response.settings.animationDuration`是`null`或`undefined`,就会返回默认值300。也就是说,这一行代码包括了两级属性的判断。

这个运算符很适合判断函数参数是否赋值。

```javascript
function Component(props) {
const enable = props.enabled ?? true;
// …
}
```

上面代码判断`props`参数的`enabled`属性是否赋值,基本等同于下面的写法。

```javascript
function Component(props) {
const {
enabled: enable = true,
} = props;
// …
}
```

`??`有一个运算优先级问题,它与`&&`和`||`的优先级孰高孰低。现在的规则是,如果多个逻辑运算符一起使用,必须用括号表明优先级,否则会报错。

```javascript
// 报错
lhs && middle ?? rhs
lhs ?? middle && rhs
lhs || middle ?? rhs
lhs ?? middle || rhs
```

上面四个表达式都会报错,必须加入表明优先级的括号。

```javascript
(lhs && middle) ?? rhs;
lhs && (middle ?? rhs);

(lhs ?? middle) && rhs;
lhs ?? (middle && rhs);

(lhs || middle) ?? rhs;
lhs || (middle ?? rhs);

(lhs ?? middle) || rhs;
lhs ?? (middle || rhs);
```

Loading