我可以理解该ES6高阶功能的逻辑流程如何
逻辑如何流向:(books)=> (架子)=> ...
const shelf1 = [
{ name: "name1", shelf: "a" },
{ name: "name2", shelf: "a" },
];
const shelf2 = [
{ name: "name3", shelf: "b" },
{ name: "name4", shelf: "b" },
];
const allBooks = [...shelf1, ...shelf2];
const filter = (books) => (shelf) => books.filter((b) => b.shelf === shelf);
const filterBy = filter(allBooks);
const booksOnShelf = filterBy("b");
我需要一个更详细的等同于这个缩短的表达,以帮助我消化这种魔术
how the logic flows at: (books) => (shelf) => ...
const shelf1 = [
{ name: "name1", shelf: "a" },
{ name: "name2", shelf: "a" },
];
const shelf2 = [
{ name: "name3", shelf: "b" },
{ name: "name4", shelf: "b" },
];
const allBooks = [...shelf1, ...shelf2];
const filter = (books) => (shelf) => books.filter((b) => b.shelf === shelf);
const filterBy = filter(allBooks);
const booksOnShelf = filterBy("b");
i need a more verbose equivalent to this shortened expression, to help me to digest that magic
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个接受
books
参数并返回 new 函数的函数,该功能接受shelf
参数。该功能分配给filterby
,并调用的结果函数(数组)被分配给booksonshelf
。内部功能将其引用
books
返回时,通常称为闭合。It's a function that accepts a
books
argument and returns a new function that accepts ashelf
argument. That function is assigned tofilterBy
, and the result of calling that function (an array) is assigned tobooksOnShelf
.The inner function maintains a reference to
books
when it's returned, and is generally called a closure.