迭代器

创建一个迭代器,接收任意多个函数参数

function nextRegister() {
    var args = arguments;
    var count = -1;
    var comm = {};
    function nextTime() {
        count++;
        if (count < args.length) {
            if (args[count] && Object.prototype.toString.call(args[count]) == '[object Function]') {
                args[count](comm, nextTime);
            }
        }
    }
    nextTime();
} 

创建多个异步的函数,注入到迭代器中

/*
         comm:多个函数,公用的变量
         next:调用下一个函数
         * */
        function fn1(comm,next){
            console.log('1');
            comm.age = 20;
            next();
        }
        function fn2(comm,next){
            next();
            console.log('2');
            console.log(comm.age);
        }
        function fn3(comm,next){
            console.log('3');
        }
 
//开始执行迭代
nextRegister(fn1,fn2,fn3);
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容