This is very thorough! Now I understand the code, Thanks a lot! But doe it mean I have to write code like this to make an ajax:
// Set defaults $.ajaxSetup({ data:{ defaultKey:"defaultValue" } }); // Make an ajax call $.ajax({ url: './index.html', data: $.ajaxSetup.ext({ customKey: "customValue" } }) This is not very neat, IMHO. On Oct 24, 1:39 am, Scott Sauyet <scott.sau...@gmail.com> wrote: > On Thu, Oct 22, 2009 at 9:57 PM, gMinuses <gminu...@gmail.com> wrote: > > I'm sorry, but I don't quite understand your code. > > It took me a while to get it, too. Obviously Andrea can tell you > better, but perhaps a mere mortal can explain it better to other > mortals. :-) > > First of all, you need to know that jQuery.ajaxSetup(obj) simply > extends the jQuery.ajaxSettings object with the content of obj, so the > call to that function is defining parameters that will be supplied in > every AJAX call. Clearly we're defining a new method named "ext" for > the jQuery.ajaxSettings object. And in the end, we're calling that > method. The result of that call will be an object that looks like > this: > > { > defaultKey:"defaultValue", > otherKey:"otherValue", > test: 123 > } > > So the only thing left is the actual definition of "ext". Nothing to > it, right? :-) > > This is the shell: > > >> jQuery.ajaxSettings.ext = (function(anonymous){ > >> // ... > >> })(Function()); > > This is calling Function(), which returns an anonymous function with > no body and no name (which might be considered something like a blank > constructor), and then passing that in as the parameter "anonymous" to > an unnamed function. "ext" will be the returned value of the body of > that shell, namely > > >> return function ext(o){ > >> // ... > >> }; > > So ext will be a function. > > Note that the re-use of the name ext in this declaration is not > necessary. It allows the function to refer to itself internally > without resorting to arguments.callee. But since it's never used > here, this could just as easily have been > > >> return function(o){ > >> // ... > >> }; > > The body of this function starts with > > >> anonymous.prototype = this.data || {}; > >> var k, result = new anonymous; > > The "this" context is jQuery.ajaxSettings, which we've set earlier to > have a parameter "data", that contains, in this case, {defaultKey: > "defaultValue", otherKey: "otherValue"}. When we set the prototype > for the anonymous constructor to that data object, all object created > from it will have these properties. Now we create a new instance of > that object, called "result". > > >> if(o){ > >> for(k in o) > >> result[k] = o[k] > >> ; > >> }; > >> return result; > > and populate that resulting object with any parameters we've passed > in, then return it. > > Now a call to a jQuery.ajax() method can be supplied the result of > jQuery.ajaxSettings.ext() call as its "data" parameter. > > Does that help? > > Andrea, is that accurate? > > -- Scott --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "jQuery Development" group. To post to this group, send email to jquery-dev@googlegroups.com To unsubscribe from this group, send email to jquery-dev+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/jquery-dev?hl=en -~----------~----~----~----~------~----~------~--~---