jquery has a very useful function queue, which we may rarely use, but he has a pivotal position within jquery.
He is callbacks. jquery authors use it to build a lot of very important modules. such as $. Deferred.
Callbacks is the number group, which contains a lot of function objects. But does he really just so?
Well, Einstein was just a person, but was he really just an ordinary person? Callbacks is not.
Don't talk nonsense, see the source code.
String to Object options format Cachevar optionscache = {};//Convert string-formatted options into object-formatted on Es and store in cachefunction createoptions (options) {var object = optionscache[Options] = {};jquery.each (options.spl It (core_rspace), function (_, flag) {object[flag] = true;}); return object;} /* * Create a callback list using the following parameters: * *options:an Optional list of space-separated options that W Ill *the callback list behaves or a more traditional option object * * By default a callback list would act like An event callback list and can is * "fired" multiple times. * * Possible Options: * *once:will Ensure the callback list can only is fired once (like a Deferred) * *memory:will Keep t Rack of previous values and would call any callback added *after the list have been fired right away with the latest "Memori Zed "*values (like a Deferred) * *unique:will ensure a callback can is only added once (no duplicate in the list) * *stopo NfalsE:interrupt callings When a callback returns false * *///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-forg ettable lists) memory,//flag to know if list were already firedfired,//Flag to know if list is currently firingfiring,//F Irst 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.M Emory && 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 Addbreak;}} Firing = False;if (list) {if (stack) {if (stack.length) {Fire (Stack.shift ());}} else if (memory) {list = [];} el SE {self.disable ();}}},//Actual Callbacks objectself = {//ADD a callback or a collection of callbacks to the Listadd:fu Nction () {if (list) {//First, we save the current lengthvar start = List.length; (function add (args) {Jquery.each (arg S, function (_, arg) {var type = 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 Listremove:function () {if (list) {Jquery.each (arguments, function (_, arg {var index;while (index = Jquery.inarray (ARG, list, index)) >-1) {List.splice (index, 1);//Handle Firing IND Exesif (firing) {if (index <= firinglength) {firinglength--;} if (index <= firingindex) {firingindex--;}}});} Return this;},//Control If a given callback is in the Listhas:function (FN) {return Jquery.inarray (FN, list) >-1; },//Remove all callbacks from the listempty:function () {list = [];return this;},//The list does nothing Anymoredisab Le:function () {list = stack = Memory = Undefined;return this;},//is it disabled?disabled:function () {return!list;},// Lock the list in it current statelock:function () {stack = undefined;if (!memory) {self.disable ();} Return this;},//is it locked?locked:function () {return!stack;},//call all callbacks with the given Context and Argumentsfirewith:function (context, args) {args = args | | [];args = [Context, Args.slice? Args.slice (): args];if (list && (!fired | | | stack)) {if (firing) {STACK.P Ush (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;}}; return self;};
The code is only 200 lines, but it really looks like it's just a little bit around,
In the think in Java, the best way to understand a program is to think of it as a service provider.
That he offered those services:
First we look at the returned self object
{//Add method Add:function () {},//Delete remove:function () {},//contains has:function () {},//empty empty:function () {},//Disable Disable:fu Nction () {},//locking lock:function () {},//is lock locked:function () {},//triggers firewith:function () {},fire:function () {},//} Ired:function () {}}
Use is very clear, then we look at the parameters, the program is the provider of the service, then the parameters as the entry of the program's carrier, usually used to assemble some properties.
Obviously this is the case here.
jquery Source Low-key callback function queue--callbacks