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

proto.off 删除对应的监听函数后为什么还要把null补进去 #6

Open
xiterjia opened this issue Jul 20, 2018 · 2 comments
Open

Comments

@xiterjia
Copy link

listeners.splice(index, 1, null)

splice从第三个参数开始不是要进行添加元素的操作么?为什么删除对应的事件后,还要把null添加进去,不太理解

@chinawzc
Copy link

应该用不着

@threedayAAAAA
Copy link

主要用在这里

proto.emit = function(eventName, args) {
        var listeners = this.__events[eventName];
        if (!listeners) return;
        for (var i = 0; i < listeners.length; i++) {
            var listener = listeners[i];
            if (listener) {
                listener.listener.apply(this, args || []);
                if (listener.once) {
                     ---> this.off(eventName, listener.listener)
                }
            }

        }

        return this;

    };

如果不填充一个null,而是直接删除的话,那下一次循环的时候会少执行一个回调

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

3 participants