【问题标题】:How to read the documentation syntaxes? [duplicate]如何阅读文档语法? [复制]
【发布时间】:2020-05-30 05:22:19
【问题描述】:

有人可以解释一下我是如何阅读这种语法的,如下所示。我在其中选择了 MDN reduce 方法参考。我想知道括号是什么意思,为什么单词前有逗号等等。

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

【问题讨论】:

  • [] 中的任何内容通常都是可选参数...逗号在可选部分内,因为要使用可选部分,您需要一个逗号...例如callback(accumulator, curretnvalue, index) 是正确的,但 callback(accumulator, curretnvalue index) 显然不是

标签: javascript syntax code-documentation


【解决方案1】:

TL;DR:括号表示“可选”。逗号用于显示不同的函数参数。您可以在 MDN 文档中查看每个属性的“可选”指示。

arr.reduce(callback( accumulator, currentValue[, index[, array]] )[, initialValue])
  • arr => Reduce 是 Array 原型上的一个函数:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce
  • callback:reduce函数的第一个参数是一个“reducer函数”。
    • 此回调有 4 个参数,其中 2 个是“可选”的。
    • 您将接受 accumulator 值作为回调的第一个参数
    • 您将接受每个数组元素作为currentValue 作为第二个参数
    • 索引和数组在[...] 中表示它们是“可选的”。除非您关心实现 reducer 的索引,否则您无需担心它们。
  • initialValue 是 reduce 函数的第二个“可选”值: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.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-20
    • 1970-01-01
    • 2013-12-24
    • 2011-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多