webpack_tapble

1、如何使用

let { SyncHook } = require("tabpable");
class Lesson {
  constructor() {
    this.hooks = {
      arch: new SyncHook(["name"]),
    };
  }
  tap() {
    //注册  监听函数
    this.hooks.arch.tap("node", function (name) {
      console.log("node", name);
    });
    this.hooks.arch.tap("react", function (name) {
      console.log("react", name);
    });
  }
  start() {
      this.hooks.arch.call('jim');
  }
}

let l = new Lesson();
l.tap(); //注册这两个事件
l.start(); //启动钩子

2、手写代码

class SyncHook {
  //钩子是同步的
  // args=>['name]
  constructor(args) {
      this.task=[];
  }
  tap(name, task) {
      this.task.push(task);
  }
  call(...args) {
    this.task.forEach((task)=>{
        return task(...args)
    })
  }
}

let hook = new SyncHook(["name"]);
hook.tap("react", function (name) {
  console.log("react", name);
});
hook.tap("node", function (name) {
  console.log("node", name);
});

hook.call('jim')

 

你可能感兴趣的:(webpack)