JS编程技巧
Jonnzer Lv4

最近看vue-router源码,有一些 JS 技巧记录下:

1 尽量写纯函数,将上下文依赖减少
2 减少主函数的体积,降低心智负担。尽量抽离模块。
3 递归 :只要有适当的触发条件,会帮上很多忙。层级结构可以打上标记,关联 parent层 和 children层;

if 或者 while 都是常用的方式

4 队列处理函数:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
* 循环运行迭代器 (相当于中间件)
* 递归执行内置next函数
*/
function runQueue(queueList, iterator, cb) {
function next (index) {
if (index >= queueList.length) {
return cb()
}
iterator(queueList[index], () => next(index + 1))
}
next(0)
}
const iterator = (hook, nextFn) => { // 迭代器 只管中间参数
hook(newCurrent, this.current, nextFn)
}
runQueue(hooksQueue, iterator, () => {
// callback()
}
  • 本文标题:JS编程技巧
  • 本文作者:Jonnzer
  • 创建时间:2022-04-14 17:33:45
  • 本文链接:https://jonnzer.github.io/2022/04/14/编程技巧/编程技巧/
  • 版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
 评论