1. 程式人生 > >jquery源碼01---(2880 , 3042) Callbacks : 回調對象 : 對函數的統一管理

jquery源碼01---(2880 , 3042) Callbacks : 回調對象 : 對函數的統一管理

-1 刪除 core call 如果 可選 args json fire

// optionsCache : { ‘once memory‘ : { once : true , memory : true } }
var optionsCache = {};

// once memory,options.match( core_rnotwhite )=[once, memory],function( _, flag )={once:true,memory:true}
function createOptions( options ) {
    var object = optionsCache[ options ] = {};//中括號就是json的點,沒有點了。
    jQuery.each( options.match( core_rnotwhite ) || [], function
( _, flag ) { object[ flag ] = true; }); return object; } /* var cb = $.Callbacks(); function aaa(){ alert(1); } cb.add(aaa); (function(){ function bbb(){ alert(2); } cb.add(bbb); })(); cb.fire();//1 2 */ jQuery.Callbacks
= function( options ) {//類的靜態方法 //options 可選: once memory unique stopOnFalse //方法:add remove has empty disable disabled lock locked fireWith fire fired //什麽都不寫走jQuery.extend( {}, options ),返回空{},不這樣寫,如果options===undefined,後面還要做兼容。 options = typeof options === "string" ? ( optionsCache[ options ]
|| createOptions( options ) ) : jQuery.extend( {}, options );//options={once:true,memory:true} var memory, fired, firing, firingStart, firingLength, firingIndex, // 添加的所有方法 list = [], stack = !options.once && [], fire = function( data ) { memory = options.memory && data;//有memory返回true fired = true;//觸發開始 firingIndex = firingStart || 0; firingStart = 0; firingLength = list.length; firing = true; for ( ; list && firingIndex < firingLength; firingIndex++ ) { if ( list[ firingIndex ].apply( data[ 0 ], data[ 1 ] ) === false && options.stopOnFalse ) {// 函數返回false,有stopOnFalse屬性就不向下執行 memory = false; //memory也要置為false break; } } firing = false;//觸發結束 if ( list ) { if ( stack ) { if ( stack.length ) { fire( stack.shift() ); } } else if ( memory ) { list = []; } else { self.disable(); } } }, self = {//對外部接口 add: function() { if ( list ) {//list一上來是空數組,會返回真, var start = list.length; (function add( args ) {//args是形參 jQuery.each( args, function( _, arg ) {//遍歷傳進來的多個方法名, var type = jQuery.type( arg ); if ( type === "function" ) { if ( !options.unique || !self.has( arg ) ) {//不是unioqye,或者是unipue但是沒有 list.push( arg ); } } else if ( arg && arg.length && type !== "string" ) {//數組,嵌套 add( arg ); } }); })( arguments );//arguments是實參,方法名aaa, if ( firing ) { firingLength = list.length; } else if ( memory ) {//第一次沒有賦值是undefined, firingStart = start; fire( memory ); } } return this; }, remove: function() { if ( list ) { jQuery.each( arguments, function( _, arg ) {//可以刪除多個 var index; while( ( index = jQuery.inArray( arg, list, index ) ) > -1 ) { list.splice( index, 1 );//刪除 if ( firing ) { if ( index <= firingLength ) { firingLength--; } if ( index <= firingIndex ) { firingIndex--; } } } }); } return this; }, has: function( fn ) { return fn ? jQuery.inArray( fn, list ) > -1 : !!( list && list.length ); }, empty: function() { list = []; firingLength = 0; return this; }, disable: function() { list = stack = memory = undefined; return this; }, disabled: function() { return !list; }, lock: function() { stack = undefined; if ( !memory ) { self.disable(); } return this; }, locked: function() { return !stack; }, fireWith: function( context, args ) { if ( list && ( !fired || stack ) ) { args = args || []; args = [ context, args.slice ? args.slice() : args ]; if ( firing ) { stack.push( args ); } else { fire( args ); } } return this; }, fire: function() { self.fireWith( this, arguments );//cb.fire(‘hello‘); return this; }, fired: function() { return !!fired; } }; return self; };

jquery源碼01---(2880 , 3042) Callbacks : 回調對象 : 對函數的統一管理