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

來源:互聯網
上載者:User

標籤:lag   obj   art   context   elf   each   remove   開始   賦值   

// 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 : 回調對象 : 對函數的統一管理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.