Home > Article > Web Front-end > Native js implements simple encapsulation method
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!