event emitter 库_event指令

event emitter 库_event指令!DOCTYPEhtmlhtmllang=”en”headmetacharset=”UTF-8″titleDocument/title/headbodyscripttype=”text

event

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <script type="text/javascript">
        function EventEmitter() {
            this.events = {}
        }   
        // on 监听事件 off移除监听事件 emit
        //on off emit   
        // on("xxx", function(){}) 
        // off("xxx", fn)
        // emit("xxx") 
        // 
        EventEmitter.prototype.once = function(event, fn) {
            if(event) {
                this.events[event] = this.events[event]?this.events[event] : []
                function wrapFn() {
                    this.off(event, fn)
                    fn.apply(this, arguments)
                }
                wrapFn.fn = fn
                this.events[event].push(wrapFn)
            }
            return this
        }
              
        EventEmitter.prototype.on = function(event, fn) {
            if(event) {
                this.events[event] = this.events[event]?this.events[event] : []
                //this.events["xxx"] = []
                //
                //
                this.events[event].push(fn)
            }
            return this
        }

        EventEmitter.prototype.off = function(event, fn) {
            if(event) {
                if(fn) {
                    var eventHanlders = this.events[event]
                    for(var i=0;i<eventHanlders.length;) {
                        if(eventHanlders[i] === fn || eventHanlders[i].fn === fn) {
                            eventHanlders.splice(i, 1)
                        } else {
                            i++
                        }
                    }
                } else {
                    this.events[event] = []
                }
            }
            return this
            
        }

        EventEmitter.prototype.emit = function(event) {
            if(event) {
                var args = [].slice.call(arguments, 1)

                var eventHanlders = this.events[event]
                for(var i=0;i<eventHanlders.length;i++) {
                    eventHanlders[i].apply(this, args)
                }
            }
            return this;
        }

        var ee = new EventEmitter()
        var fn = function() {
            console.log(arguments)
            console.log("fn")
        }
        var fn1 = function() {
            console.log("fn1")
        }
        var aaa = function() {
            console.log('aaa')
        }

        ee.on("xxx", fn).on("aaa", aaa).on('xxx', fn1)
        ee.emit("xxx", 'hahaha').emit("aaa")

        ee.off("xxx", fn)
        console.log('-------')
        ee.emit("xxx")

        /*ee.off("xxx")

        ee.on("aaa", fn)
        ee.emit("aaa")
        ee.off("aaa")*/

        ee.once("aa", function(){console.log('aaaaaaaaa')})
        ee.emit('aa')
        ee.emit('aa')

    </script>
</body>
</html>

 

今天的文章event emitter 库_event指令分享到此就结束了,感谢您的阅读。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/78425.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注