I’m reading the deferred object in jQuery. Could anyone please tell me what’s the difference between following two invoking way?
$.when.apply(null, a method).done(function(){success callback})$.when.(a method).done(function(){success callback})
And what kind of cases are fit for the first way above?
Thanks in advance.
$.when.apply(null, a method)only makes sense if a method is actually an array or a method call returning an array. Then it’s like a$.when(elements, of, the, array). See MDN for a detailed description of theapplymethod.$.when.(a method)makes no sense at all, but I guess you meant$.when(a method). In this case a method should again be a method call returning a deferred object or a variable that points to a deferred object.The syntax of
$.when()is$.when(one, or, more, deferreds)– so if you want to pass multiple deferreds which are in an array, you need.apply()since you don’t want to build the method call as a string and useeval(which is indeed evil in this case).