標籤:c style class code java a
JQuery並沒有簡單的使用一個Array來儲存回呼函數,而是通過JQuery.Callbacks(options)返回一個self對象,此對象可以動態add,remove和fire回呼函數隊列.此函數需要說明的是options參數,它是一個string,這個string由四個參數任意組合而成options:once:回呼函數只執行一次memory:調用add時觸發回呼函數使用fire時傳入的參數.unique:回呼函數只能被添加到隊列一次stopOnFlase:有一個回呼函數返回false,停止執行下面的回呼函數.
JQuery.Callbacks(options)內的變數根據不同傳參有不同的意思,這些函數由閉包引用擴大其生命週期,以在外部改變其值,以下進行說明memory:如果options不包含‘memory‘字串,memeory為false,其他為上一次fire(args)的參數.fired:回呼函數列表是否被觸發過.firing:回呼函數列表是否正在被觸發.為何要這個參數呢?因為如果在回呼函數內調用了add,remove,fire 方法時會改變回呼函數列表的狀態(開始觸發的位置,觸發長度等等,這些是控制要執行回呼函數列表中哪些函數的標誌),此時其回呼函數函數列表正在處於觸發中,必須可以及時反映(修正觸發位置,觸發長度等等)回呼函數列表的變化,以便在回呼函數執行的迴圈中下一次迭代可以正確的觸發回呼函數列表.firingStart:回呼函數列表執行的開始位置.firingLength:再執行幾個回呼函數firingIndex:正在觸發的函數在回呼函數列表中的位置list:存放回呼函數stack:如果在回呼函數中調用fire或fireWith方法,將fire或fireWith傳入的上下文和參數先入棧,稍後再用棧中的參數來作為參數執行回呼函數中調用的fire或fireWidth。
註:不解的是走到stack.push分支時是在回呼函數裡調用fire或fireWidth,但這樣會導致死迴圈,這個stack到底怎麼回事?還請明白的同學告知我一下!!
jQuery.Callbacks = function( options ) { // Convert options from String-formatted to Object-formatted if needed // (we check in cache first) options = typeof options === "string" ? ( optionsCache[ options ] || createOptions( options ) ) : jQuery.extend( {}, options ); var // Last fire value (for non-forgettable lists) memory, // Flag to know if list was already fired fired, // Flag to know if list is currently firing firing, // First callback to fire (used internally by add and fireWith) firingStart, // End of the loop when firing firingLength, // Index of currently firing callback (modified by remove if needed) firingIndex, // Actual callback list list = [], // Stack of fire calls for repeatable lists // 如果沒有配置once,stack為數組 // stack存放重複時fire參數 stack = !options.once && [], // Fire callbacks fire = function( data ) { //memory 為false或fire時傳入的參數 memory = options.memory && data; fired = true; //當前觸發位置 firingIndex = firingStart || 0; //觸發回呼函數隊列的起始位置 firingStart = 0; //待觸發的回呼函數隊列長度 firingLength = list.length; //正在觸發標識設定為true //只有在Callbacks的回呼函數裡此值才會為true firing = true; for ( ; list && firingIndex < firingLength; firingIndex++ ) { //若配置了stopOnFalse當回呼函數隊列中函數傳回值為false if ( list[ firingIndex ].apply( data[ 0 ], data[ 1 ] ) === false && options.stopOnFalse ) { //memory設定為false memory = false; // To prevent further calls using add //不再執行後面的回呼函數 break; } } //回呼函數執行結束 firing = false; if ( list ) { //未配置once if ( stack ) { //執行在回呼函數裡調用的fire if ( stack.length ) { fire( stack.shift() ); } //清空回呼函數列表,但此callbacks仍可用 } else if ( memory ) { list = []; //讓閉包的那些引用可以銷毀 } else { self.disable(); } } }, // Actual Callbacks object self = { // Add a callback or a collection of callbacks to the list add: function() { if ( list ) { // First, we save the current length // 將添加回呼函數之前將當前callbacks的長度設為執行開始 var start = list.length; ( function add( args ) { jQuery.each( args, function( _, arg ) { var type = jQuery.type( arg ); if ( type === "function" ) { // 如果未設定unique或者回呼函數列表裡沒有此函數 // 在回呼函數隊列裡加上此函數 if ( !options.unique || !self.has( arg ) ) { list.push( arg ); } // 如果arg是個類數組,再遞迴add } else if ( arg && arg.length && type !== "string" ) { // Inspect recursively add( arg ); } }); })( arguments ); // Do we need to add the callbacks to the // current firing batch? //在正在觸發的回呼函數中調用add方法會走此分支 if ( firing ) { //立刻反應要觸發回呼函數長度的改變 firingLength = list.length; // With memory, if we're not firing then // we should call right away } else if ( memory ) { //改變觸發的開始位置為未添加前的回調隊列長度 firingStart = start; fire( memory ); } } return this; }, // Remove a callback from the list remove: function() { if ( list ) { jQuery.each( arguments, function( _, arg ) { //arg所在list的索引 var index; while ( ( index = jQuery.inArray( arg, list, index ) ) > -1 ) { //刪除 list.splice( index, 1 ); // Handle firing indexes //如果在回呼函數裡調用remove if ( firing ) { //要刪除的回呼函數是還沒觸發的 if ( index <= firingLength ) { //觸發的長度減一 firingLength--; } //要刪除的是已經觸發過的函數 if ( index <= firingIndex ) { //正要觸發索引減一 firingIndex--; } } } }); } return this; }, // Check if a given callback is in the list. // If no argument is given, return whether or not list has callbacks attached. has: function( fn ) { return fn ? jQuery.inArray( fn, list ) > -1 : !!( list && list.length ); }, // Remove all callbacks from the list empty: function() { list = []; firingLength = 0; return this; }, // Have the list do nothing anymore //是回呼函數列表功能完全失效 disable: function() { list = stack = memory = undefined; return this; }, // Is it disabled? disabled: function() { return !list; }, // Lock the list in its current state //鎖住當前的狀態,不會執行在回呼函數裡調用fire或fireWidth lock: function() { stack = undefined; if ( ! memory ) { self.disable(); } return this; }, // Is it locked? locked: function() { return !stack; }, // Call all callbacks with the given context and arguments fireWith: function( context, args ) { //沒有被觸發或者可以重複觸發 if ( list && ( !fired || stack ) ) { args = args || []; args = [ context, args.slice ? args.slice() : args ]; //?其實這裡有一點不解,走到這個分 //是在在回呼函數裡調通fire或fireWidth,但這樣會導致死迴圈 if ( firing ) { stack.push( args ); } else { fire( args ); } } return this; }, // Call all the callbacks with the given arguments fire: function() { self.fireWith( this, arguments ); return this; }, // To know if the callbacks have already been called at least once fired: function() { return !!fired; } }; return self;};