• js实现事件模型bind与trigger


    function Emitter() {
        this._listener = [];//_listener[自定义的事件名] = [所用执行的匿名函数1, 所用执行的匿名函数2]
    }
    
    //注册事件
    Emitter.prototype.bind = function(eventName, callback) {
        var listener = this._listener[eventName] || [];//this._listener[eventName]没有值则将listener定义为[](数组)。
        listener.push(callback);
        this._listener[eventName] = listener;
    }
    
     //触发事件
    Emitter.prototype.trigger = function(eventName) {
        var args = Array.prototype.slice.apply(arguments).slice(1);//atgs为获得除了eventName后面的参数(注册事件的参数)
        var listener = this._listener[eventName];
    
        if(!Array.isArray(listener)) return;//自定义事件名不存在
        listener.forEach(function(callback) {
            try {
                callback.apply(this, args);
            }catch(e) {
                console.error(e);
            }
        })
    }
    //实例
    var emitter = new Emitter();
        emitter.bind("myevent", function(arg1, arg2) {
            console.log(arg1, arg2);
        });
    
        emitter.bind("myevent", function(arg1, arg2) {
            console.log(arg2, arg1);
        });
    
        emitter.trigger('myevent', "a", "b");
    

      实现链式调用的例子,原理与事件模型相同

    function LazyMan(name) {
                return new _LazyMan(name);
            }
            function _LazyMan(name) {
                console.log("Hi This is " + name)
                this.task = [];
                var _this = this;
                var namer = (function(name) {
                    return function() {
                        console.log(name);
                        _this.next();
                    }
                })(name)
                this.task.push(namer);
                setTimeout(function() {
                    _this.next();
                }, 0);
                return this;
            }
            _LazyMan.prototype.next = function() {
                var fn = this.task.shift();
                fn&&fn();
            }
            _LazyMan.prototype.eat = function(val) {
                var _this = this;
                var eat = (function(val) {
                    return function() {
                        console.log("eat" + val);
                        _this.next();
                    }
                })(val);
                this.task.push(eat);
                return this;
            }
            _LazyMan.prototype.sleep = function(time) {
                var _this = this;
    
                var timer = (function(time) {
                    return function() {
                        setTimeout(function() {
                            console.log("wait");  
                            console.log("time=" + time);
                            _this.next();
                        }, time*1000);
                    }
                    
                })(time);
                this.task.push(timer);
                return this;
            }
        
        //LazyMan("Hank").eat("dinner").eat("supper");
        LazyMan("Hank").sleep(3).eat("dinner");

      

  • 相关阅读:
    【题解】Acwing 90 64位整数乘法
    【题解】Acwing 89 a ^ b
    【题解】POJ 1995 Raising Modulo Numbers
    python图像处理(2)图像水印和PIL模式转化
    python图像处理(1)图像的打开与保存
    请把我风干成你的回忆
    python图像处理 模式转化简单总结
    python 不以科学计数法输出
    talib指标公式及释义整理
    cuda、cudnn下载安装教程
  • 原文地址:https://www.cnblogs.com/pcd12321/p/5223347.html
Copyright © 2020-2023  润新知