How to implement a complete event system with Javascript?

前端 未结 3 1906
走了就别回头了
走了就别回头了 2021-01-27 17:43

I\'m creating a client-side dynamic blog engine. Now I need a event system to handle many actions from DOM elements and the engine. Such as the engine is loading a article,user

3条回答
  •  佛祖请我去吃肉
    2021-01-27 18:13

    class SimpleEvent {
      constructor() {
        this.onEvent = {}
        this.handler = function(funct, name) {
          var owner = this
          var name = name
          this.onEvent[name] = funct
          var remove = function() {
            delete this.owner.onEvent[this.name]
            delete this.owner
            delete this.name
            delete this.remove
          }
          if ((((!(remove == undefined && name == undefined)) && (remove == undefined || name == undefined)))) {
            throw new Error("-_-")
          } else {
            return (remove == undefined || name == undefined) ? (undefined) : ({
              remove: remove,
              name: name
            })
          }
        }
      }
      Fire() {
        for (var i in this.onEvent) {
          this.onEvent[i](arguments)
        }
      }
    }

提交回复
热议问题