javascript-tutorial / javascript-tutorial/en.javascript.info

Lacking arguments in array.sort()'s compareFn

未关闭
#3,562 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
HTML
星标
25.5k
派生
4k
PR 合并指标
30 天内没有已合并 PR

描述

Hello, I found this weird usage in test [Shuffle an array](https://javascript.info/array-methods#shuffle-an-array)'s "simple solution":

![image](https://github.com/javascript-tutorial/en.javascript.info/assets/80401151/678c9a62-033f-40db-b9aa-2d7b57c62e3b)

There is just no arguments in the `array.sort()`'s `compareFn`:
```js
array.sort(() => Math.random() - 0.5);
```

But [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#parameters) says if we use `compareFn`, the arguments of `compareFn` "Will never be undefined.":

![image](https://github.com/javascript-tutorial/en.javascript.info/assets/80401151/27735ec7-2e97-4fb4-8aee-f9b2fa2d7d76)

According to MDN, the `compareFn` must be used with arguments, i.e.
```js
array.sort((a, b) => Math.random() - 0.5);
```

If it is a typo indeed, please fix it at your convenience, thank you.

贡献指南

这个仓库没有索引到贡献指南

调研方向

从 issue 中链接的“Shuffle an array”部分开始,在教程源代码中搜索所示的 array.sort() 示例。对照所引用的 MDN 文档确认 compareFn 的措辞,如果示例中存在拼写错误则更新示例,并验证教程显示的是修正后的代码片段。

由索引模型根据 Issue 内容生成。

评估

技术栈
javascript
领域
documentation
Issue 类型
文档
难度
2/5
预计耗时
1-3 小时
活跃度
停滞
描述清晰度
基本清楚
新手友好度
48/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。