有人可以解释我如何阅读以下语法。我在其中选择MDN减少方法参考。我想知道方括号的意思,为什么在单词等之前加逗号。
arr.reduce(callback( accumulator, currentValue[, index[, array]] )[, initialValue])
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/reducecallback
:归约函数的第一个参数是“归约函数”。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.