jquery Source Research and analysis learning notes-callback functions (11)

Source: Internet
Author: User
Tags parse string

A callback function is a function that is called through a function pointer. If you pass the pointer (address) of the function as an argument to another function, when the pointer invokes the function it points to, we say this is a callback function. The callback function is not called directly by the implementing party of the function, but is invoked by another party when a particular event or condition occurs, and is used to respond to the event or condition.

//Tools function, convert string format tokens to object format, and cache the results of the conversion. function createflags(Flags) {    //Initialize return value object and Flagscache[flags] are empty objects, pointing to the same empty object, and adding properties to Flagscache[flags when the variable object is added. There is no need to write another line of code to put the variable object into the Cache object Flagscache.    varObject = flagscache[Flags] = {}, I, length; Flags = Flags.split (/\s+/);//Divide the token string into arrays with whitespace characters    //Iterate through an array, add a single tag for the return value of object, a property value of true, and finally return an object     for(i =0, length = Flags.length; i < length; i++) {object[Flags[i]] =true; }returnobject;} Jquery.callbacks = function(flags) {    //Parse string marked as Flags object, first gets the token string from the cache object Flagscache flags corresponding to the tag object, and if not found, invokes the tool function CreateFlags (flags) to parse the token string flags into the tag object , and put in the shabby object FlagscacheFlags = flags? (flagscache[flags] | | createflags (FLAGS)): {};var //Declare local variables, reference by closure mechanism        List= [],//array that holds the callback function        //On a repeatable trigger, on a list that is being executed, the context and parameters are placed in the array stack when the trigger is repeatedstack = [], memory,// Fired,////The list of callback functions is being executedFiring,//subscript for the first callback function to be executedFiringstart,//End of the loop when firingFiringlength,//Index of currently firing callback (modified by remove if needed)Firingindex,//Add a callback function to the tool functionAdd = function(args) {            varI, length, elem, type, actual;//Traversal parameter args, add the callback function to the array list one by one,             for(i =0, length = Args.length; i < length;                i++) {elem = args[i]; Type = Jquery.type (elem);///judgment Args[i] type, and if it is an array, then iterate calls the Rule function add (args) adds the callback function in the array to the array list,                if(Type = = ="Array") {Add (Elem); }Else if(Type = = ="function") {//If it is a function and is not a unique mode, or is a unique mode but not added, Args[i] is added to the list in the array                    if(!flags.unique | |! Self. has (Elem)) {List. push (Elem); }                }            }        },The tool function that triggers the callback function, using context contexts and parameter args, invokes the callback function in the array list, which references the array list by the closure mechanism        The//parameter context is used to specify the contexts in which the callback function executes, that is, the object referenced by the keyword this, and the parameter args is used to specify the arguments passed in when the callback function is calledFire = function(context, args) {args = args | | [];//If the current callback function list is not memory mode, the variable memory is assigned true, indirectly indicating that the current list of callback functions has been triggeredMemory =!flags.memory | |            [Context, args]; Fired =true; Firing =true; Firingindex = Firingstart | |0; Firingstart =0; Firinglength =List. length; for( ;List&& Firingindex < Firinglength; firingindex++) {//Execute callback function list[Firingindex], if the return value is false and the current list of callback functions is Stoponfalse mode, the variable memory is assigned True and the subsequent callback function is stopped .                if(List[Firingindex].apply (context, args) = = =false&& flags.stoponfalse) {memory =true;//Mark as halted                     Break; }} firing =false;if(List) {//If the callback function list can be triggered multiple times if it is not in once mode, then the entire list of callback functions is executed again by ejecting the stored context and parameters from the variable stack until the stack is empty                if(!flags.once) {if(Stack && stack.length) {memory = Stack.shift (); Self. Firewith (memory[0], memory[1] ); }                }Else if(Memory = = =true) { Self. disable (); }Else{List= []; }            }        },///callback function list, method Jquery.callbacks (Flags) return value         Self= {//Add callback functionAdd function() {                if(List) {//Backup array list length first                    varLength =List. length; Add (arguments);//If the callback function is executing, fix end subscript firinglength so that the newly added callback function is executed                    if(firing) {firinglength =List. length;// }Else if(Memory && Memory!==true) {Firingstart = length; Fire (memory[0], memory[1] ); }                }returnThis },//Remove callback function, remove one or a set of callback functions from the list of callback functions, remove the pre-fixup end subscript firinglength and the current off-duty FiringindexRemove function() {                if(List) {varargs = arguments, Argindex =0, arglength = Args.length;//Traverse the array of callback functions to be removed, nesting in the loop body to iterate through an existing array of callback functions,                     for(; argindex < arglength; argindex++) { for(vari =0; I <List. length; i++) {//If you check that a pending callback function is exactly equal to an existing callback function, remove it from the existing array of callback functions                            if(args[argindex] = = =List[i]) {///callback function is in the list                                if(firing) {//If the callback function list is executing if (i <= firinglength) {            //Use end subscript firinglength minus 1 firinglength--prior to removal;            //If the subscript of the function to be removed is less than the subscript firingindex that is executing the callback function, that is, the callback function to be removed has been executed, then the correction Firingindex minus 1 to ensure that the callback function if (i <) is not missed ; = Firingindex) {firingindex--; }                                    }                                }//Call array prototype method splice (), remove 1 elements from the matching subscript, loop variable i minus one, and fix the subscript for the next callback function                                List. Splice (I--,1); ///In unique mode, there is no duplicate callback function in the array list, you can exit the inner traversal directly, and optimize for the unique mode .                                if(Flags.unique) { Break; }                            }                        }                    }                }returnThis },The //callback function is all in the listHas: function(FN) {                if(List) {vari =0, length =List. length; for(; i < length; i++) {if(fn = = =List[i]) {return true; }                    }                }return false; },//Clear list            Empty: function() {                List= [];returnThis },//Disable listDisable function() {                List= Stack = memory = undefined;returnThis },//Whether the list is disabledDisabled function() {                return!List; },//Lock listLock function() {stack = undefined;if(!memory | | memory = = =true) { Self. disable (); }returnThis },//Whether the list has been wrongly fixedLocked function() {                return!stack; },//Invoke callback function with specified context and parametersFirewith: function(context, args) {                if(Stack) {if(firing) {if(!flags.once)                        {Stack.push ([context, args]); }                    }Else if( ! (flags.once && memory))                    {Fire (context, args); }                }returnThis },invokes the callback function with the specified argument, and the context is selfFire: function() {                 Self. Firewith (this, arguments);returnThis },///callback function list has been executed at least onceFired: function() {                return!!            Fired; }        };return  Self;};

jquery Source Research and analysis learning notes-callback functions (11)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.