javascript event util #js #event

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript event util #js #event相关的知识,希望对你有一定的参考价值。

function handleEvent (eventName, {onElement, withCallback, useCapture = false} = {}, thisArg) {
  const element = onElement || document.documentElement

  function handler (event) {
    if (typeof withCallback === 'function') {
      withCallback.call(thisArg, event)
    }
  }

  handler.destroy = function () {
    return element.removeEventListener(eventName, handler, useCapture)
  }

  element.addEventListener(eventName, handler, useCapture)
  return handler
}

// Anytime you need
const handleClick = handleEvent('click', {
  onElement: element,
  withCallback: (event) => {
    console.log('Tada!')
  }
})

// And anytime you want to remove it
handleClick.destroy()

以上是关于javascript event util #js #event的主要内容,如果未能解决你的问题,请参考以下文章

轻松学 Node.js

Javascript流体布局

node.js 下使用 util.inherits 来实现继承

javascript:关于js的function参数中带的Event

javascript events.js

mac终端运行nodejs总是提示events.js:183错误(内详)到达怎么解决?