JavaScript performance library

Concept of cascades in JavaScript is very simple. Take a look at the following code:

cascade.
    setManufacturer("Hyundai").
    setModel("Coupe").
    setColor("Red").
    setEngine("2.7 v6").
    describe();

Notice how each of the methods are executed. Every of those methods does not to have to return anything specific. If we have those methods return this, we can create cascades. In a cascade, we can invoke multiple methods on the same object in sequence in a single statement.

Learn how to create cascades in JavaScript by reading this article.

Take a look at cascade object definition (note: this object uses a closure, read more about closures here):

var cascade = function() {
    var manufacturer = "";
    var model = "";
    var color = "";
    var engine = "";
    
    return {
        setManufacturer: function(text) {
            manufacturer = text;
            return this;
        },
        setModel: function(text) {
            model = text;
            return this;
        },
        setColor: function(text) {
            color = text;
            return this;
        },
        setEngine: function(text) {
            engine = text;
            return this;
        },
        describe: function() {
            alert(color+" "+manufacturer+" "+model+" "+engine);
            return this;
        }
    }
}();

You can notice that every method returns this which is bound to the object containing all the methods (read more about this behavior here). The return statement does not return a copy of the object, but reference to it. This is what enables to execute cascaded method invocations on the same object.

Here is a JSFiddle link with the full source code of this example.