javascript - JS:处理列表时的“前”和“后”操作(使用 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)
})
})
在这里,“安装”操作在开始时仅触发一次。'列表' 并且仅在处理数字数组'arr'的开始时应用一次。
有没有办法使用 IIFE 来抽象“一次”行为?
我们能否以同样的方式进行 endong 安装后任务?
注意:这个问题是关于处理的“方面”。因为它将适用于 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)
}
因为循环中的测试成本(就复杂性而言)......
问候。
解决方案
推荐阅读
- unit-testing - 初始化循环 Golang
- qt - 操作的快捷方式不显示
- .htaccess - 我可以在共享托管 Apache 服务器上使用 RewriteMap 吗?
- c# - WebAPI 控制器看不到价值
- javascript - 项目映射时获取键和项目 onPress TouchableOpacity
- go - 在 Go 中使用 google oauth2
- c# - 导航按钮在 wpf 中无法正常工作
- typemock-isolator - 在 TypeMock 中模拟受保护的属性
- php - 使用 laravel artisan 迁移时出现 PHP 致命错误
- c# - 没有 App.config 的实体框架,数据库优先,存储库模式,N 层解决方案架构