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 = [];
                }
            }
        }
    
  • 相关阅读:
    pop指令的执行过程
    ipad 4.2 开发环境
    XP远程桌面
    停机问题、哥德尔定理
    看MIT的线性代数
    VizA项目继续推进
    port systemc to mac os x
    关于jar
    普桑
    拉突爾
  • 原文地址:https://www.cnblogs.com/heyinwangchuan/p/6259309.html
Copyright © 2011-2022 走看看