如何阅读文档语法?

问题描述 投票:0回答:1

有人可以解释我如何阅读以下语法。我在其中选择MDN减少方法参考。我想知道方括号的意思,为什么在单词等之前加逗号。

arr.reduce(callback( accumulator, currentValue[, index[, array]] )[, initialValue])

javascript syntax code-documentation
1个回答
1
投票

TL; DR:方括号表示“可选”。存在逗号以显示不同的功能参数。您可以在MDN文档中看到每个属性的“可选”指示。

arr.reduce(callback( accumulator, currentValue[, index[, array]] )[, initialValue])
  • arr => Reduce是数组原型上的一个函数:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce
  • callback:归约函数的第一个参数是“归约函数”。
    • 此回调有4个参数,其中2个为“可选”。
    • 您将接受accumulator值作为回调的第一个参数
    • 您将接受每个数组元素作为currentValue作为第二个参数
    • 索引和数组在[...]中,表示它们是“可选的”。您无需担心它们,除非您关心实现减速器的索引。
  • initialValue是归约函数的第二个“可选”值:A value to use as the first argument to the first call of the callback. If no initialValue is supplied, the first element in the array will be used as the initial accumulator value and skipped as currentValue. Calling reduce() on an empty array without an initialValue will throw a TypeError.
© www.soinside.com 2019 - 2024. All rights reserved.