Home > Web Front-end > JS Tutorial > body text

Native js implements simple encapsulation method

小云云
Release: 2018-02-28 11:42:50
Original
1746 people have browsed it

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);
  };
};
Copy after login

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

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

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

removeFN();
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template