如何解决JS:处理列表时的“pre”和“post”操作使用 IIFE ?
考虑以下将“列表”作为参数并相应地对数组执行操作的代码:
# ready to test: just copy it in a file...
const create = (list) => {
console.log('seting up for list',list)
const install = (pool) => console.log('installing',pool)
const nope = (pool) => void 0
const action = (t) => console.log('action',t)
const api = (tasks) => {
return {
install: (pool) => {
tasks.install(pool)
// the TURN is structurally made HERE (no if...)
tasks = { install: nope }
return { action }
}
}
}
let once = { install }
return api(once)
}
const bmp = { 'blocks-memoty-pool': 1234 }
const lists = ['a,b,c','d,e,f']
const arr = [0,1,2,3]
lists.map((l) => {
console.log()
const created = create(l)
arr.map((e) => {
created.install(bmp).action(e)
})
})
此处,“安装”操作仅在开始时触发一次。 'list' 中,并且仅在处理数字数组 'arr' 的最开始应用一次。
有没有办法使用 IIFE 来抽象“一次”行为?
我们可以用同样的方式进行安装后任务吗?
注意: 这个问题是关于处理的“方面”。因为它将适用于 Async/Await 用例..我必须避免类似的事情:
for(let i = 0; i < arr.length; i++) {
if(i === 0) pre_install(nmp)
process(arr[i])
if(i === arr.length - 1) post_install(nmp)
}
由于循环中的测试成本(就复杂性而言)...
问候。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。