Skip to content

Commit ec5d683

Browse files
committed
docs: add some ES2025 syntax
1 parent 5821b9f commit ec5d683

File tree

4 files changed

+37
-6
lines changed

4 files changed

+37
-6
lines changed

docs/arraybuffer.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
简单说,`ArrayBuffer`对象代表原始的二进制数据,`TypedArray`视图用来读写简单类型的二进制数据,`DataView`视图用来读写复杂类型的二进制数据。
1818

19-
`TypedArray`视图支持的数据类型一共有 9 种(`DataView`视图支持除`Uint8C`以外的其他 8 种)
19+
`TypedArray`视图支持的数据类型一共有12种
2020

2121
| 数据类型 | 字节长度 | 含义 | 对应的 C 语言类型 |
2222
| -------- | -------- | -------------------------------- | ----------------- |
@@ -27,6 +27,9 @@
2727
| Uint16 | 2 | 16 位不带符号整数 | unsigned short |
2828
| Int32 | 4 | 32 位带符号整数 | int |
2929
| Uint32 | 4 | 32 位不带符号的整数 | unsigned int |
30+
| BigInt64 | 8 | 64 位有符号整数 | |
31+
| BigUint64 | 8 | 64 位无符号整数 | |
32+
| Float16 | 2 | 16 位浮点数 | |
3033
| Float32 | 4 | 32 位浮点数 | float |
3134
| Float64 | 8 | 64 位浮点数 | double |
3235

@@ -153,10 +156,13 @@ ArrayBuffer.isView(v) // true
153156
- **`Uint16Array`**:16 位无符号整数,长度 2 个字节。
154157
- **`Int32Array`**:32 位有符号整数,长度 4 个字节。
155158
- **`Uint32Array`**:32 位无符号整数,长度 4 个字节。
159+
- **`BigInt64Array`**: 64 位有符号整数,长度 8 个字节。
160+
- **`BigUint64Array`**:64 位无符号整数,长度 8 个字节。
161+
- **`Float16Array`**: 16 位浮点数,长度 2 个字节。
156162
- **`Float32Array`**:32 位浮点数,长度 4 个字节。
157163
- **`Float64Array`**:64 位浮点数,长度 8 个字节。
158164

159-
这 9 个构造函数生成的数组,统称为`TypedArray`视图。它们很像普通数组,都有`length`属性,都能用方括号运算符(`[]`)获取单个元素,所有数组的方法,在它们上面都能使用。普通数组与 TypedArray 数组的差异主要在以下方面。
165+
这12个构造函数生成的数组,统称为`TypedArray`视图。它们很像普通数组,都有`length`属性,都能用方括号运算符(`[]`)获取单个元素,所有数组的方法,在它们上面都能使用。普通数组与 TypedArray 数组的差异主要在以下方面。
160166

161167
- TypedArray 数组的所有成员,都是同一种类型。
162168
- TypedArray 数组的成员是连续的,不会有空位。
@@ -165,7 +171,7 @@ ArrayBuffer.isView(v) // true
165171

166172
### 构造函数
167173

168-
TypedArray 数组提供 9 种构造函数,用来生成相应类型的数组实例。
174+
TypedArray 数组提供12种构造函数,用来生成相应类型的数组实例。
169175

170176
构造函数有多种用法。
171177

@@ -746,7 +752,7 @@ const dv = new DataView(buffer);
746752
- `DataView.prototype.byteLength`:返回占据的内存字节长度
747753
- `DataView.prototype.byteOffset`:返回当前视图从对应的 ArrayBuffer 对象的哪个字节开始
748754

749-
`DataView`实例提供10个方法读取内存
755+
`DataView`实例提供11个方法读取内存
750756

751757
- **`getInt8`**:读取 1 个字节,返回一个 8 位整数。
752758
- **`getUint8`**:读取 1 个字节,返回一个无符号的 8 位整数。
@@ -756,6 +762,7 @@ const dv = new DataView(buffer);
756762
- **`getUint32`**:读取 4 个字节,返回一个无符号的 32 位整数。
757763
- **`getBigInt64`**:读取 8 个字节,返回一个 64 位整数。
758764
- **`getBigUint64`**:读取 8 个字节,返回一个无符号的 64 位整数。
765+
- **`getFloat16`**:读取 2 个字节,返回一个 16 位浮点数。
759766
- **`getFloat32`**:读取 4 个字节,返回一个 32 位浮点数。
760767
- **`getFloat64`**:读取 8 个字节,返回一个 64 位浮点数。
761768

@@ -790,7 +797,7 @@ const v2 = dv.getUint16(3, false);
790797
const v3 = dv.getUint16(3);
791798
```
792799

793-
DataView 视图提供10个方法写入内存
800+
DataView 视图提供11个方法写入内存
794801

795802
- **`setInt8`**:写入 1 个字节的 8 位整数。
796803
- **`setUint8`**:写入 1 个字节的 8 位无符号整数。
@@ -800,6 +807,7 @@ DataView 视图提供10个方法写入内存。
800807
- **`setUint32`**:写入 4 个字节的 32 位无符号整数。
801808
- **`setBigInt64`**:写入 8 个字节的 64 位整数。
802809
- **`setBigUint64`**:写入 8 个字节的 64 位无符号整数。
810+
- **`setFloat16`**:写入 2 个字节的 16 位浮点数。
803811
- **`setFloat32`**:写入 4 个字节的 32 位浮点数。
804812
- **`setFloat64`**:写入 8 个字节的 64 位浮点数。
805813

docs/number.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,21 @@ Math.hypot(-3); // 3
652652

653653
如果参数不是数值,`Math.hypot`方法会将其转为数值。只要有一个参数无法转为数值,就会返回 NaN。
654654

655+
### Math.f16round()
656+
657+
ES2025 新增了 Math.f16round() 方法,返回最接近输入值的16位半精度浮点数。
658+
659+
```javascript
660+
Math.f16round(5) // 5
661+
Math.f16round(5.05) // 5.05078125
662+
```
663+
664+
16位浮点数共使用16个二进制位,其中指数使用5位,符号位使用1位,精度使用10位,因此可以表示 ±65,504 范围内的值,精度可以到达 1/1024。如果一个数超出了值的范围,则该方法返回 infinity。
665+
666+
```javascript
667+
Math.f16round(100000) // Infinity
668+
```
669+
655670
### 对数方法
656671

657672
ES6 新增了 4 个对数相关方法。

docs/promise.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1082,7 +1082,7 @@ console.log('next');
10821082

10831083
上面代码也是使用立即执行的匿名函数,执行`new Promise()`。这种情况下,同步函数也是同步执行的。
10841084

1085-
鉴于这是一个很常见的需求,所以现在有一个[提案](https://github.com/ljharb/proposal-promise-try),提供`Promise.try`方法替代上面的写法。
1085+
鉴于这是一个很常见的需求,所以 [ES2025](https://github.com/ljharb/proposal-promise-try) 提供了`Promise.try()`方法替代上面的写法。
10861086

10871087
```javascript
10881088
const f = () => console.log('now');

docs/regex.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,14 @@ matchObj.groups.as // undefined
582582

583583
上面代码中,具名组`as`没有找到匹配,那么`matchObj.groups.as`属性值就是`undefined`,并且`as`这个键名在`groups`是始终存在的。
584584

585+
如果使用`|`运算符,给出两种可选方案,那么同样名称的组匹配,可以使用两次。其他情况,同一个名字的组匹配都只能出现一次。
586+
587+
```javascript
588+
const RE = /(?<chars>a+)|(?<chars>b+)/v;
589+
```
590+
591+
上面示例中,具名组匹配`<chars>``|`前后使用了两次。
592+
585593
### 解构赋值和替换
586594

587595
有了具名组匹配以后,可以使用解构赋值直接从匹配结果上为变量赋值。

0 commit comments

Comments
 (0)