zoukankan      html  css  js  c++  java
  • 10天掌握webpack 4.0 tapable (1) SyncHook 的原理

    class SyncHook {
      constructor(args) {
        this.tasks = []
      }
      tap(name, task) {
        this.tasks.push(task)
      }
      call(...args) {
        this.tasks.forEach((task) => {
          task(...args)
        })
      }
    }
    let hook = new SyncHook(['name']);
    hook.tap('react', function (name) {
      console.log('react', name)
    })
    hook.tap('vue', function (name) {
      console.log('vue', name)
    })
    hook.tap('node', function (name) {
      console.log('node', name)
    })
    hook.call('jw')
    

      1. 我们先声明一个空数组 这个数组来存放 注册事件

          2.  tap 是用来 将每个注册的方法添加到 数组中 的 

          

     this.tasks.push(task)

     3.   call  方法 

      call 遍历数组 我们 得到一个个的 函数 调用每个函数 绑定

     this.tasks.forEach((task) => {
          task(...args)
     })
  • 相关阅读:
    维度穿梭
    演绎与抽象
    幻想的功能
    深层探宝
    内存游戏
    函数内功
    共享与私有的变量
    参数的格式
    功能模拟与功能实现
    【Oracle】基础知识查漏补缺
  • 原文地址:https://www.cnblogs.com/guangzhou11/p/12507318.html
Copyright © 2011-2022 走看看