jQuery源碼研究分析學習筆記-回呼函數(11)

來源:互聯網
上載者:User

標籤:

回呼函數就是一個通過函數指標調用的函數。如果你把函數的指標(地址)作為參數傳遞給另一個函數,當這個指標調用它所指向的函數時,我們就說這是回呼函數。回呼函數不是由該函數的實現方直接調用,而是在特定的事件或條件發生時由另外的一方調用的,用於對該事件或條件進行響應。

// 工具函數,將字串格式的標記轉換為對象格式,並把轉換結果緩衝起來function createFlags( flags ) {    //初始化傳回值object和flagsCache[flags]為空白對象,同時指向了同一個Null 物件,當變數object添加屬性時也是在為flagsCache[flags]添加屬性,不需要要再寫一行代碼把變數object放入緩衝對象flagsCache中了    var object = flagsCache[ flags ] = {},        i, length;    flags = flags.split( /\s+/ ); //用空白符把標記字串分割為數組    //遍曆數組,為傳回值object添加單個標記,屬性值為true,最後返回對象    for ( i = 0, length = flags.length; i < length; i++ ) {        object[ flags[i] ] = true;    }    return object;}jQuery.Callbacks = function( flags ) {    // 解析字串標記為flags為對象,先從緩衝對象flagsCache中擷取標記字串flags對應的標記對象,如果沒有找到,在調用工具函數createFlags(flags)將標記字串flags解析為標記對象,並放入寒磣對象flagsCache中    flags = flags ? ( flagsCache[ flags ] || createFlags( flags ) ) : {};    var // 聲明局部變數,通過閉包機制引用        list = [],//存放回呼函數的數組        //在可重複觸發、正在執行的列表上,重複觸發時,將上下文和參數放入數組stack中        stack = [],        memory,        //         fired,        // 回呼函數列表是否正在執行中        firing,        // 待執行的第一個回呼函數的下標        firingStart,        // End of the loop when firing        firingLength,        // Index of currently firing callback (modified by remove if needed)        firingIndex,        // 添加回呼函數的工具函數        add = function( args ) {            var i,                length,                elem,                type,                actual;                //遍曆參數args,把回呼函數逐個添加到數組list中,            for ( i = 0, length = args.length; i < length; i++ ) {                elem = args[ i ];                type = jQuery.type( elem );                //判斷args[i]類型,如果是數組,則迭代調用規矩函數add(args)把數組中的回呼函數添加到數組list中,                if ( type === "array" ) {                    add( elem );                } else if ( type === "function" ) {                    // 如果是函數且不是unique模式,或者是unique模式但未添加過,才會添加args[i]到數組中list中                    if ( !flags.unique || !self.has( elem ) ) {                        list.push( elem );                    }                }            }        },        // 觸發回呼函數的工具函數,使用上下文context和參數args調用數組list中的回呼函數,該回呼函數通過閉包機制引用數組list        //參數context用於指定回呼函數執行時的上下文,即關鍵字this所引用的對象,參數args用於指定調用回呼函數時傳入的參數        fire = function( context, args ) {            args = args || [];            //若當前回呼函數列表不是memory模式,則變數memory則被賦值為true,間接地表示當前回呼函數列表已經被觸發過            memory = !flags.memory || [ context, args ];            fired = true;            firing = true;            firingIndex = firingStart || 0;            firingStart = 0;            firingLength = list.length;            for ( ; list && firingIndex < firingLength; firingIndex++ ) {                //執行回呼函數list[ firingIndex ],如果傳回值是false,且當前回呼函數列表是stopOnFalse模式,則變數memory被賦值為true,並停止執行後續的其他回呼函數,                if ( list[ firingIndex ].apply( context, args ) === false && flags.stopOnFalse ) {                    memory = true; // Mark as halted                    break;                }            }            firing = false;            if ( list ) {            //如果不是once模式,即可以多次觸發回呼函數列表,則從變數stack中彈出存放的下文和參數,再次執行整個回呼函數列表,直到stack為空白                if ( !flags.once ) {                    if ( stack && stack.length ) {                        memory = stack.shift();                        self.fireWith( memory[ 0 ], memory[ 1 ] );                    }                } else if ( memory === true ) {                    self.disable();                } else {                    list = [];                }            }        },        // 回呼函數列表,方法jQuery.Callbacks(flags)的傳回值        self = {            // 添加回呼函數            add: function() {                if ( list ) {                    //先備份數組list長度                    var length = list.length;                    add( arguments );                    // 如果回呼函數正在執行,修正結束下標firingLength,使得新添加的回呼函數也得以執行                    if ( firing ) {                        firingLength = list.length;                    //                     } else if ( memory && memory !== true ) {                        firingStart = length;                        fire( memory[ 0 ], memory[ 1 ] );                    }                }                return this;            },            // 移除回呼函數,從回呼函數列表中移除一個或一組回呼函數,移除前修正結束下標firingLength和當前下班firingIndex            remove: function() {                if ( list ) {                    var args = arguments,                        argIndex = 0,                        argLength = args.length;                    //遍曆待移除的回呼函數數組,在迴圈體內嵌套遍曆已有的回呼函數數組,                    for ( ; argIndex < argLength ; argIndex++ ) {                        for ( var i = 0; i < list.length; i++ ) {                        //如果檢查到某個待移除回呼函數與某個已有的回呼函數完全相等,則從已有的回呼函數數組中移除它                            if ( args[ argIndex ] === list[ i ] ) {                                // 回呼函數是否在列表中                                if ( firing ) {        //如果回呼函數列表正在執行                          if ( i <= firingLength ) {            //在移除前使用結束下標firingLength減1                          firingLength--;            //如果待移除函數的下標小於正在執行回呼函數的下標firingIndex,即待移除的回呼函數已經執行,則修正firingIndex減1,以確保不會漏執行回呼函數                            if ( i <= firingIndex ) {                                            firingIndex--;                                        }                                    }                                }                                // 調用數組原型方法splice(),從匹配下標處開始移除1個元素,迴圈變數i自減一,修正為下一個回呼函數的下標                                list.splice( i--, 1 );                                /                                // 在unique模式下,數組list中不會有重複的回呼函數,可以直接退出內層遍曆,針對unique模式做的最佳化                                if ( flags.unique ) {                                    break;                                }                            }                        }                    }                }                return this;            },            // 回呼函數是都在列表中            has: function( fn ) {                if ( list ) {                    var i = 0,                        length = list.length;                    for ( ; i < length; i++ ) {                        if ( fn === list[ i ] ) {                            return true;                        }                    }                }                return false;            },            // 清空列表            empty: function() {                list = [];                return this;            },            // 禁用列表            disable: function() {                list = stack = memory = undefined;                return this;            },            // 是否已禁用列表            disabled: function() {                return !list;            },            // 鎖定列表            lock: function() {                stack = undefined;                if ( !memory || memory === true ) {                    self.disable();                }                return this;            },            // 是否已錯定列表            locked: function() {                return !stack;            },            // 使用指定的上下文和參數調用回呼函數            fireWith: function( context, args ) {                if ( stack ) {                    if ( firing ) {                        if ( !flags.once ) {                            stack.push( [ context, args ] );                        }                    } else if ( !( flags.once && memory ) ) {                        fire( context, args );                    }                }                return this;            },            // 使用指定的參數調用回呼函數,上下文為self             fire: function() {                self.fireWith( this, arguments );                return this;            },            // 回呼函數列表是否至少執行過一次            fired: function() {                return !!fired;            }        };    return self;};

jQuery源碼研究分析學習筆記-回呼函數(11)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.