See the next sizzle part of the source code, the core principle is to use regular expression to match, find the corresponding method of the original acquisition element, I did not go to scrutiny. Everyone is interested to see for yourself, share and share!
Start with 2850 lines and Continue reading jquery source (2850-3043 lines)
Before entering the callbacks (callback function Management module), there are several extension methods
1.dir method
Three parameters: Elem--dom element, dir--specifies Elem hierarchy name (for example parentnode,nextsibling), until--end judgment. Returns an array, such as the parentnode of an element, that returns an array of parent elements of the HTML, if until is not specified. Here you can know that the parent element of HTML is document,document the parent element is null--no parent element.
2.pushStack: Simulates a stack operation, generating a new jquery object.
3.jquery.callbacks: List of callback functions, management function module
Line: 3066-3228
Jquery.callbacks =function(options) {//Convert options from string-formatted to object-formatted if needed //(We check in cache first)Options =typeofoptions = = = "string"?(optionscache[options]||createoptions (Options)): Jquery.extend ({}, Options); var //Last fire value (for non-forgettable lists)Memory,//Flag to know if list is already firedfired,//Flag to know if list is currently firingFiring,//First callback to fire (used internally by add and Firewith)Firingstart,//End of the loop when firingFiringlength,//Index of currently firing callback (modified by remove if needed)Firingindex,//Actual Callback ListList = [], //Stack of Fire calls for repeatable listsstack =!options.once && [], //Fire CallbacksFire =function(data) {memory= Options.memory &&data; 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) {memory=false;//To prevent further calls using Add Break; }} Firing=false; if(list) {if(Stack) {if(stack.length) {Fire (Stack.shift ()); } } Else if(memory) {list= []; } Else{self.disable (); } } }, //Actual Callbacks ObjectSelf = { //Add a callback or a collection of callbacks to the listAddfunction() { if(list) {//First , we save the current length varStart =list.length; (functionAdd (args) {Jquery.each (args,function(_, arg) {varType =Jquery.type (ARG); if(Type = = = "function" ) { if(!options.unique | |!Self.has (ARG)) {List.push (ARG); } } Else if(Arg && arg.length && type!== "string" ) { //Inspect recursivelyAdd (ARG); } }); }) (arguments); //Do we need to add the callbacks to the //Current firing batch? 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 listRemovefunction() { if(list) {Jquery.each (arguments,function(_, arg) {varindex; while(index = Jquery.inarray (ARG, list, index)) >-1) {list.splice (index,1 ); //Handle Firing Indexes 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) {returnFn? Jquery.inarray (FN, list) >-1:!! (List &&list.length); }, //Remove all callbacks from the listEmptyfunction() {list= []; Firinglength= 0; return This; }, //The list does nothing anymoreDisablefunction() {list= Stack = Memory =undefined; return This; }, //is it disabled?Disabledfunction() { return!list; }, //Lock the list in it current stateLockfunction() {stack=undefined; if( !memory) {self.disable (); } return This; }, //is it locked?Lockedfunction() { return!Stack; }, //Call all callbacks with the given context and argumentsFirewith: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; }, //Call all the callbacks with the given argumentsFire:function() {Self.firewith ( This, arguments); return This; }, //To know if the callbacks has already been called at least onceFired:function() { return!!fired; } }; returnSelf ;};
View Code
3.1 Parameters options, translate the comments as follows:
Once: Make sure that the list of callback functions is triggered only once, memory: records the last parameter that was triggered, and the callback function that is added after that is immediately called with the parameter value.
Unique: The same callback function can only be added once, to avoid repeating the list of callback functions; Stoponfalse: Break execution callback function when one of the callback functions returns false
3.2 var rnotwhite= (/\s+/g/): matches a non-whitespace character.
This is primarily to convert the options string format to object format and cache (Optionscache)
For example "once Memory" through CreateOptions ("Once Memory") ==>{"Once": "true", "Memory": "True"}
3.3 Main included two functions fire and self,callbacks internally define a list array to hold the lists of callback functions
3.3.1 fire: Triggers all callback function execution by list[firingindex].apply (data[0], data[1]) to execute the function, and two parameters are: Context and function parameters, respectively.
3.3.2 Self: Add,remove the callback function list
4. Line 3231-3461 deferred and when (asynchronous queue module)
4.1 Deferred: With respect to callbacks, there are 3 function lists, each representing success, failure, and message callbacks.
vartuples = [ //action, add Listener, listener list, final state["Resolve", "done", Jquery.callbacks ("Once Memory"), "resolved" ], [ "Reject", "fail", Jquery.callbacks ("Once Memory"), "rejected" ], [ "Notify", "Progress", Jquery.callbacks ("Memory")]],... jquery.each (tuples,function(i, tuple) {varList = tuple[2], statestring= tuple[3 ]; //promise[Done | fail | progress] = List.addpromise[tuple[1]] = List.add;
A list of 3 functions is generated by traversing tuples and added to the promise in the form of the Add method. Promise is a read-only copy of an asynchronous queue and is only responsible for adding and judging, not changing the state and triggering execution. The promise also defines a promise method with the same name, which is responsible for returning a promise copy, then a shortcut--Adding a list of 3 functions in a method (success, failure, message). It then adds a pipe attribute to the promise to point to then. After you create a read-only copy of deferred, you start by adding a modified state to the deferred object and a method that triggers execution.
4.2 When: For deferred again encapsulation, as the name implies, when all the successful callback function completed, and then do something. Based on a count flag, each time a successful callback function is executed, the--1 is executed when the calculator ==0.
4.3 finally returns a primary asynchronous queue object return Deferred.promise ();
Cond...
Read the jquery source Four (Callbacks,deferred,when)