Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
20 changes: 0 additions & 20 deletions docs/zh_hans/reference/function/spread.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,3 @@ console.log(spreadMultiply.call(calculator, [2, 3, 4])); // 24
#### 返回值

(`(args: Parameters<F>) => ReturnType<F>`): 返回一个接收参数数组并以展开形式传递给原始函数的新函数。

## 与 Lodash 的兼容性

从 `es-toolkit/compat` 导入 `spread` 可与 lodash 兼容。

- `spread` 额外接收一个名为 `argsIndex` 的数字参数。此参数表示要展开的参数数组的给定索引。
- 如果 `argsIndex` 为负数或 `NaN`,则视为默认值 `0`。如果是小数,则向下舍入到最接近的整数。

```typescript
import { spread } from 'es-toolkit/compat';

function fn(a: unknown, b: unknown, c: unknown) {
return Array.from(arguments);
}

spread(fn, -1)([1, 2]); // Returns [1, 2]
spread(fn, NaN)([1, 2]); // Returns [1, 2]
spread(fn, 'a')([1, 2]); // Returns [1, 2]
spread(fn, 1.6)(1, [2, 3]); // Returns [1, 2, 3]
```
58 changes: 0 additions & 58 deletions docs/zh_hans/reference/function/throttle.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,61 +98,3 @@ throttledFunc.cancel();
#### 返回值

(`ThrottledFunction<F>`): 返回执行被限制的新函数。包含 `cancel` 和 `flush` 方法。

## 与 Lodash 的兼容性

从 `es-toolkit/compat` 导入 `throttle` 可与 lodash 完全兼容。

- `throttle` 函数接收 `leading` 和 `trailing` 选项。

- `leading`: 首次调用节流函数时是否立即执行原始函数。默认值为 `true`。
- `trailing`: 自最后一次调用节流函数后经过 `throttleMs` 毫秒后是否执行原始函数。默认值为 `true`。

- `throttleMs` 选项的默认值为 `0`。意味着函数调用只延迟到下一个 tick。

::: info `throttle` 的 `leading` 和 `trailing` 选项

默认情况下 `throttle` 的 `leading` 和 `trailing` 选项为 `true`。因此像 `{ leading: true }` 或 `{ trailing: true }` 这样的选项不会改变函数的行为。

:::

```typescript
// leading 选项示例
const leadingFn = throttle(
() => {
console.log('Leading function executed');
},
1000,
{ leading: true }
);

// 立即打印 'Leading function executed'
// 即使继续调用也每秒打印一次 'Leading function executed'
leadingFn();

// trailing 选项示例
const trailingFn = throttle(
() => {
console.log('Trailing function executed');
},
1000,
{ trailing: true }
);

// 立即打印 'Trailing function executed'
// 即使继续调用也每秒打印一次 'Trailing function executed'
trailingFn();

// leading: false, trailing: true 选项示例
const trailingOnlyFn = throttle(
() => {
console.log('Trailing-only function executed');
},
1000,
{ leading: false, trailing: true }
);

// 'Trailing-only function executed' 最初不会打印
// 即使继续调用也每秒打印一次 'Trailing-only function executed'
trailingOnlyFn();
```