Home  >  Article  >  Web Front-end  >  Native js implements simple encapsulation method

Native js implements simple encapsulation method

小云云
小云云Original
2018-02-28 11:42:501738browse

This article mainly shares with you the simple encapsulation method of native js. I hope the code shared in this article can help you.

const ListenerFN = function ({ target, event, FN, option = {} }) {
  if (target, event, FN) Error('target, event, FN');
  target.addEventListener(event, FN, option);
  return function () {
    target.removeEventListener(event, FN);
  };
};

Pass in the dom object, trigger the event, and callback method, you need to pass the parameters;

const removeFN = ListenerFN({
      target,
      FN,
      event,
 });

Returns a method to remove the binding, and after executing it again, the binding will be removed;

removeFN();

Related recommendations:

Explanation of simple encapsulation examples for multi-file downloads in WeChat mini programs

php simply encapsulates some common JS operations_ PHP tutorial

Detailed introduction to ajax simple encapsulation

The above is the detailed content of Native js implements simple encapsulation method. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn