• A enhanced curry method

    JavaScript is a functional language, it supports function composition, we can do curry with JavaScript. Douglas Crockford has defined "curry" method in his book JavaScript: The Good Parts to facilitate the steps to define curried function.

    Function.prototype.method = function ( name, func ) {
     if ( !this.prototype[name] ) {
      this.prototype[name] = func;
     }
    };
    
    
  • the when method

    In jQuery 1.5, there is a "when" method.

    function when(objects) {}
    
    
  • the promise object

    The jQuery.Deferred object has a function .promise(), it returns a promise object. Some other people call it future. In the async framework of .net, it is called Task<T> . It an object that promise to deliver a value in the future. So if believe that promise, you can express your believing like this


    promise.done(function () {
      var valuesOfFuture = [].slice.call(arguments, 0);
       alert();
    });
    


    The promise object does not has method of "resolve" and "reject". Because these method belong to server side. This pattern is very popular, but jQuery stream line this pattern. It is very useful pattern.

  • jQuery.Deferred Object

    Internally, the Deferred object is implemented with two _deferred object. The Deferred object offer two set of functions, done/resolveWtih/resolve/isResolved and fail/rejectWith/reject/isRejected. The are basically the functions of two internal _deferred object. The failed set function is the done/resolveWith/resoved/isREsolved function of fail object. But we don't have cancel function, because it is unnecessary, it is replaced with reject function. The done function will empty the failed list, and the reject function will empty the done list.


    It also introduced two new functions, then and promise. The then function is short cut to push a done function and failed function at one shot. We care about the success and compensation at the same time, we should consistently use then function instead of interweaving done or fail method. But if we don't care about that, we can interweave done and fail method. However, I think the then method should always be used, as it adds very little cost.


    Another new method is promise method. It basically return a new object or decorate an existing object with "promise prospect" or promise methods then, done, fail, isResolved, isRejected, promise. The promise object does the same thing as Deferred object, except it can not resolve, reject, which should be used at the server side. When we first call the d.promise(obj) with a real obj, then the obj is augmented with the promise methods and cached internally, and this pattern should be used at the first time. The subsequent call d.promise() will return the cached object.


    The constructor Deferred(initialize) can be passed with an initialize function. Your function can be defined as


    function (deferred) {
      //this == deferred
       this.then(x, y);
       this.m1 = "m1";
    }
    
  • jQuery._Deferred Object

    The jQuery._Deferred is an object maintaining a task list. To understanding it, help you to understand how the jQuery.Deferred and jQuery.when works. The _Deferred object has the following use case.


    • Push your task into the list to be run(resolved) in the future. It has been resolved, call the resolved method again.