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

狂风中的少年 提交于 2019-11-28 18:07:49

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);
}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!