这个Javascript程序的第6行让我很困惑

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

在下面的函数

takeNormal()
中,有一行我根本看不懂。是:

var end = reduce(Math.min, text.length, map(indexOrEnd, ["*", "{"]));

具体来说,在这个reduce函数中,它有所有这些不同的参数(它们是参数吗?)。它们是如何通过reduce处理的?是一一处理的吗?或者,例如,

Math.min
text.length
有什么作用吗? reduce 中的不同部分如何相互作用?reduce 最终对它们做了什么?

function splitParagraph(text) {
  function indexOrEnd(character) {
    var index = text.indexOf(character);
    return index == -1 ? text.length : index;
  }

  function takeNormal() {
    var end = reduce(Math.min, text.length,
                     map(indexOrEnd, ["*", "{"]));
    var part = text.slice(0, end);
    text = text.slice(end);
    return part;
  }

  function takeUpTo(character) {
    var end = text.indexOf(character, 1);
    if (end == -1)
      throw new Error("Missing closing '" + character + "'");
    var part = text.slice(1, end);
    text = text.slice(end + 1);
    return part;
  }

  var fragments = [];

  while (text != "") {
    if (text.charAt(0) == "*")
      fragments.push({type: "emphasised",
                      content: takeUpTo("*")});
    else if (text.charAt(0) == "{")
      fragments.push({type: "footnote",
                      content: takeUpTo("}")});
    else
      fragments.push({type: "normal",
                      content: takeNormal()});
  }
  return fragments;
}
javascript
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.