zoukankan      html  css  js  c++  java
  • jquery1.7.2的源码分析(三)$.Deferred

    例子的详细讲解

    
    
    

    上面的的代码是怎么运行的呢

    点击button的先执行 $.Deferred(),得到具有很多方法的defer

    defer.resolve( 5 );

    var doneList = jQuery.Callbacks( "once memory" ),
    failList = jQuery.Callbacks( "once memory" ),
    progressList = jQuery.Callbacks( "memory" ),
    state = "pending";
    lists = {
    resolve: doneList,
    reject: failList,
    notify: progressList
    }
    for ( key in lists ) {
    deferred[ key ] = lists[ key ].fire;
    deferred[ key + "With" ] = lists[ key ].fireWith;
    }
    //根据上面的代码可得
    //deferred[ 'resolve']=lists[ 'resolve'].fire;
    //deferred[ 'resolveWidth']=lists[ 'resolve'].fireWith;
    //deferred[ 'reject']=lists[ 'reject'].fire;
    //deferred[ 'rejectWidth']=lists[ 'reject'].fireWith;
    //deferred[ 'notify']=lists[ 'notify'].fire;
    //deferred[ 'notifyWidth']=lists[ 'notify'].fireWith;
    //因此resolve( 5 )执行了
    


    fire: function() {
        self.fireWith( this, arguments );
        return this;
    },
    fireWith: function( context, args ) {
    //stack=[];为true;firing 为false;flag={}
        if ( stack ) {
            if ( firing ) {
                if ( !flags.once ) {
                    stack.push( [ context, args ] );
                }
    //memory =undefined
            } else if ( !( flags.once && memory ) ) {
                fire( context, args );
            }
        }
        return this;
    }
    
    fire = function( context, args ) {
            args = args || [];
    //memory = [ context, args ];即为self,和5
            memory = !flags.memory || [ context, args ];
            fired = true;
            firing = true;
            firingIndex = firingStart || 0;
            firingStart = 0;
            firingLength = list.length;
            for ( ; list && firingIndex < firingLength; firingIndex++ ) {
    //函数执行后为false并且flags.stopOnFalse 为true时memory = true;
    //注意这里执行了函数
                if ( list[ firingIndex ].apply( context, args ) === false && flags.stopOnFalse ) {
                    memory = true; // Mark as halted
                    break;
                }
            }
            firing = false;
            if ( list ) {
                if ( !flags.once ) {
                    if ( stack && stack.length ) {
                        memory = stack.shift();
                        self.fireWith( memory[ 0 ], memory[ 1 ] );
                    }
                } else if ( memory === true ) {
                    self.disable();
                } else {
    //最终又把list函数组给赋值为空
                    list = [];
                }
            }
        }
    
  • 相关阅读:
    深入剖析ASP.NET的编译原理之一:动态编译(Dynamical Compilation)
    ASP.NET Process Model之二:ASP.NET Http Runtime Pipeline Part I
    IIS Server Variables(IIS 服务器变量)
    .Net源码之Page类(二)
    博客改变生活
    目录、路径、虚拟路径
    Windows 7 Beta 1 7000
    《Visual Studio Hacks 》读书笔记 (十二)
    新建XML文件
    另一个分页函数
  • 原文地址:https://www.cnblogs.com/heyinwangchuan/p/6259309.html
Copyright © 2011-2022 走看看