常见函数封装(自用,随时更新)

1.事件绑定与事件销毁

  /*
   *说明:为了绑定事件的时候,不支持传递scope.
   *参数:绑定目标,绑定事件,绑定函数,scrop常用this
   *返回:函数(用于销毁绑定)
   */
function connectEvent(target, event_name, fn, scope) { if (!target.on || typeof target.on != 'function') return; if (!fn || !event_name) return; const $fn = (e) => { fn.call(scope || null, e); } target.on(event_name, $fn); return $fn; } /* *说明:为了避免解绑定事件的时候,fn传递空导致解绑所有类型的事件封装 *参数:绑定目标,绑定事件,销毁上述函数 */

function disconnectEvent(target, event_name, fn) { if (!target.un || typeof target.un != 'function') return; if (!fn || !event_name) return; target.un(event_name, fn); }
原文地址:https://www.cnblogs.com/lucio110/p/11418355.html