Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

重复添加事件问题 #1

Open
xxxgitone opened this issue Dec 15, 2017 · 1 comment
Open

重复添加事件问题 #1

xxxgitone opened this issue Dec 15, 2017 · 1 comment

Comments

@xxxgitone
Copy link
Contributor

您好,我发现那个indexOf有点问题,不能避免重复添加事件

function indexOf(array, item) {
        if (array.indexOf) {
            return array.indexOf(item);
        } else {
            var result = -1;
            for (var i = 0, len = array.length; i < len; i++) {
                if (array[i] === item) {
                    result = i;
                    break;
                }
            }
            return result;
        }
    }

在添加事件的时候,使用indexOf(listeners, listener) === -1判断listeners没有listener才添加,但是listener可能为object,也可能为function,所以我修改为以下

function indexOf (array, item) {
    var result = -1
    item = typeof item === 'object' 
      ? item.listener 
      : item
    
    for (var i = 0, len = array.length; i < len; i++) {
      if (array[i].listener === item) {
        result = i
        break
      }
    }

    return result
  }

不知道分析的对不对?

@mqyqingfeng
Copy link
Owner

@xxxgitone 啊,确实有问题,感谢指出~ o( ̄▽ ̄)d 欢迎发起一个 Pull requrest ,我会 merge 的~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants