雄辩的Javascript第6章中的示例6.3需要这么复杂吗?
我将添加一个免责声明,除非您熟悉,否则此上下文可能没有意义 在的在线版中,我试图理解代码示例6.3的解释 该示例专门介绍了splitparation()函数,但是我在代码示例中添加了所有附加函数,使其在本文底部的示例中独立工作,但具体地说,我试图理解为什么作者建议千方百计寻找字符串的长度 作者建议使用A):雄辩的Javascript第6章中的示例6.3需要这么复杂吗?,javascript,Javascript,我将添加一个免责声明,除非您熟悉,否则此上下文可能没有意义 在的在线版中,我试图理解代码示例6.3的解释 该示例专门介绍了splitparation()函数,但是我在代码示例中添加了所有附加函数,使其在本文底部的示例中独立工作,但具体地说,我试图理解为什么作者建议千方百计寻找字符串的长度 作者建议使用A): var end = reduce(Math.min, text.length, map(indexOrEnd, ["*", "{"])); var end = text.length;
var end = reduce(Math.min, text.length, map(indexOrEnd, ["*", "{"]));
var end = text.length;
为什么不直接使用B):
var end = reduce(Math.min, text.length, map(indexOrEnd, ["*", "{"]));
var end = text.length;
我尝试过使用B),结果似乎是一样的。我可以弄清楚A)到底是怎么回事,但我不明白这样做有什么意义
这是完整的代码转储,我在底部做了一个修改,如果有人复制并粘贴它,我会将其输出:
function reduce(combine, base, array) {
forEach(array, function (element) {
base = combine(base, element);
});
return base;
}
function forEach(array, action) {
for (var i = 0; i < array.length; i++)
action(array[i]);
}
function map(func, array) {
var result = [];
forEach(array, function (element) {
result.push(func(element));
});
return result;
}
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;
}
console.log(splitParagraph("hello world"));
function reduce(联合收割机、基地、阵列){
forEach(数组、函数(元素){
底座=联合收割机(底座、元件);
});
返回基地;
}
函数forEach(数组、操作){
对于(var i=0;i
使用方法A在出现{
或*
时中断字符串。因此,SplitParagation函数将中断包含这些字符的文本
i、 e
x将包含
[
{ type: "normal", content "hello boy " },
{ type: "emphasised", content "you're the best" },
{ type: "normal", content " friend " },
{ type: "footnote", content "for me" }
]
[
{ type: "normal", content "Hello boy *you're the best* friend {for me}" }
]
使用方法Bx将包含
[
{ type: "normal", content "hello boy " },
{ type: "emphasised", content "you're the best" },
{ type: "normal", content " friend " },
{ type: "footnote", content "for me" }
]
[
{ type: "normal", content "Hello boy *you're the best* friend {for me}" }
]
使用方法A在出现
{
或*
时中断字符串。因此,SplitParagation函数将中断包含这些字符的文本
i、 e
x将包含
[
{ type: "normal", content "hello boy " },
{ type: "emphasised", content "you're the best" },
{ type: "normal", content " friend " },
{ type: "footnote", content "for me" }
]
[
{ type: "normal", content "Hello boy *you're the best* friend {for me}" }
]
使用方法Bx将包含
[
{ type: "normal", content "hello boy " },
{ type: "emphasised", content "you're the best" },
{ type: "normal", content " friend " },
{ type: "footnote", content "for me" }
]
[
{ type: "normal", content "Hello boy *you're the best* friend {for me}" }
]