docs: add some ES2025 syntax
Some checks failed
ES6 tutorial CI / Generating pages (push) Has been cancelled

This commit is contained in:
ruanyf
2025-06-28 23:15:13 +08:00
parent 5821b9f912
commit ec5d683af0
4 changed files with 37 additions and 6 deletions

View File

@@ -16,7 +16,7 @@
简单说,`ArrayBuffer`对象代表原始的二进制数据,`TypedArray`视图用来读写简单类型的二进制数据,`DataView`视图用来读写复杂类型的二进制数据。
`TypedArray`视图支持的数据类型一共有 9 种(`DataView`视图支持除`Uint8C`以外的其他 8 种)
`TypedArray`视图支持的数据类型一共有12种
| 数据类型 | 字节长度 | 含义 | 对应的 C 语言类型 |
| -------- | -------- | -------------------------------- | ----------------- |
@@ -27,6 +27,9 @@
| Uint16 | 2 | 16 位不带符号整数 | unsigned short |
| Int32 | 4 | 32 位带符号整数 | int |
| Uint32 | 4 | 32 位不带符号的整数 | unsigned int |
| BigInt64 | 8 | 64 位有符号整数 | |
| BigUint64 | 8 | 64 位无符号整数 | |
| Float16 | 2 | 16 位浮点数 | |
| Float32 | 4 | 32 位浮点数 | float |
| Float64 | 8 | 64 位浮点数 | double |
@@ -153,10 +156,13 @@ ArrayBuffer.isView(v) // true
- **`Uint16Array`**16 位无符号整数,长度 2 个字节。
- **`Int32Array`**32 位有符号整数,长度 4 个字节。
- **`Uint32Array`**32 位无符号整数,长度 4 个字节。
- **`BigInt64Array`**: 64 位有符号整数,长度 8 个字节。
- **`BigUint64Array`**64 位无符号整数,长度 8 个字节。
- **`Float16Array`**: 16 位浮点数,长度 2 个字节。
- **`Float32Array`**32 位浮点数,长度 4 个字节。
- **`Float64Array`**64 位浮点数,长度 8 个字节。
9 个构造函数生成的数组,统称为`TypedArray`视图。它们很像普通数组,都有`length`属性,都能用方括号运算符(`[]`)获取单个元素,所有数组的方法,在它们上面都能使用。普通数组与 TypedArray 数组的差异主要在以下方面。
12个构造函数生成的数组,统称为`TypedArray`视图。它们很像普通数组,都有`length`属性,都能用方括号运算符(`[]`)获取单个元素,所有数组的方法,在它们上面都能使用。普通数组与 TypedArray 数组的差异主要在以下方面。
- TypedArray 数组的所有成员,都是同一种类型。
- TypedArray 数组的成员是连续的,不会有空位。
@@ -165,7 +171,7 @@ ArrayBuffer.isView(v) // true
### 构造函数
TypedArray 数组提供 9 种构造函数,用来生成相应类型的数组实例。
TypedArray 数组提供12种构造函数,用来生成相应类型的数组实例。
构造函数有多种用法。
@@ -746,7 +752,7 @@ const dv = new DataView(buffer);
- `DataView.prototype.byteLength`:返回占据的内存字节长度
- `DataView.prototype.byteOffset`:返回当前视图从对应的 ArrayBuffer 对象的哪个字节开始
`DataView`实例提供10个方法读取内存。
`DataView`实例提供11个方法读取内存。
- **`getInt8`**:读取 1 个字节,返回一个 8 位整数。
- **`getUint8`**:读取 1 个字节,返回一个无符号的 8 位整数。
@@ -756,6 +762,7 @@ const dv = new DataView(buffer);
- **`getUint32`**:读取 4 个字节,返回一个无符号的 32 位整数。
- **`getBigInt64`**:读取 8 个字节,返回一个 64 位整数。
- **`getBigUint64`**:读取 8 个字节,返回一个无符号的 64 位整数。
- **`getFloat16`**:读取 2 个字节,返回一个 16 位浮点数。
- **`getFloat32`**:读取 4 个字节,返回一个 32 位浮点数。
- **`getFloat64`**:读取 8 个字节,返回一个 64 位浮点数。
@@ -790,7 +797,7 @@ const v2 = dv.getUint16(3, false);
const v3 = dv.getUint16(3);
```
DataView 视图提供10个方法写入内存。
DataView 视图提供11个方法写入内存。
- **`setInt8`**:写入 1 个字节的 8 位整数。
- **`setUint8`**:写入 1 个字节的 8 位无符号整数。
@@ -800,6 +807,7 @@ DataView 视图提供10个方法写入内存。
- **`setUint32`**:写入 4 个字节的 32 位无符号整数。
- **`setBigInt64`**:写入 8 个字节的 64 位整数。
- **`setBigUint64`**:写入 8 个字节的 64 位无符号整数。
- **`setFloat16`**:写入 2 个字节的 16 位浮点数。
- **`setFloat32`**:写入 4 个字节的 32 位浮点数。
- **`setFloat64`**:写入 8 个字节的 64 位浮点数。

View File

@@ -652,6 +652,21 @@ Math.hypot(-3); // 3
如果参数不是数值,`Math.hypot`方法会将其转为数值。只要有一个参数无法转为数值,就会返回 NaN。
### Math.f16round()
ES2025 新增了 Math.f16round() 方法返回最接近输入值的16位半精度浮点数。
```javascript
Math.f16round(5) // 5
Math.f16round(5.05) // 5.05078125
```
16位浮点数共使用16个二进制位其中指数使用5位符号位使用1位精度使用10位因此可以表示 ±65,504 范围内的值,精度可以到达 1/1024。如果一个数超出了值的范围则该方法返回 infinity。
```javascript
Math.f16round(100000) // Infinity
```
### 对数方法
ES6 新增了 4 个对数相关方法。

View File

@@ -1082,7 +1082,7 @@ console.log('next');
上面代码也是使用立即执行的匿名函数,执行`new Promise()`。这种情况下,同步函数也是同步执行的。
鉴于这是一个很常见的需求,所以现在有一个[提案](https://github.com/ljharb/proposal-promise-try)提供`Promise.try`方法替代上面的写法。
鉴于这是一个很常见的需求,所以 [ES2025](https://github.com/ljharb/proposal-promise-try) 提供`Promise.try()`方法替代上面的写法。
```javascript
const f = () => console.log('now');

View File

@@ -582,6 +582,14 @@ matchObj.groups.as // undefined
上面代码中,具名组`as`没有找到匹配,那么`matchObj.groups.as`属性值就是`undefined`,并且`as`这个键名在`groups`是始终存在的。
如果使用`|`运算符,给出两种可选方案,那么同样名称的组匹配,可以使用两次。其他情况,同一个名字的组匹配都只能出现一次。
```javascript
const RE = /(?<chars>a+)|(?<chars>b+)/v;
```
上面示例中,具名组匹配`<chars>``|`前后使用了两次。
### 解构赋值和替换
有了具名组匹配以后,可以使用解构赋值直接从匹配结果上为变量赋值。