Mastering JavaScript Function Composition: From Simple to Multi-Function Compose

This article explains how to build and use compose functions in JavaScript—including two‑argument, variadic, and prototype‑based versions—demonstrates practical examples, and highlights the benefits of pure functions and functional composition for modular, testable frontend code.

WeChatFE
WeChatFE
WeChatFE
Mastering JavaScript Function Composition: From Simple to Multi-Function Compose

In the previous article we introduced function currying and showed how it can simplify interfaces, enable code reuse, and produce single‑parameter (unary) functions with readable names. This follow‑up focuses on code composition , a technique that combines functions to create new, reusable pipelines.

1 Compose Function

Below is a basic two‑argument compose function that takes two functions and returns a new function applying them from right to left:

var compose = function(f, g) {
    return function(x) {
        return f(g(x));
    };
};

This version only accepts two functions, which may be insufficient when more functions need to be chained.

A variadic version can accept any number of functions and compose them sequentially:

var compose = function(){
    var args = arguments,
        start = args.length - 1; // index of the last function
    return function(){
        var i = start,
            result = args[start].apply(this, arguments); // execute the last function first
        while(i--){
            result = args[i].call(this, result); // feed result into the previous function
        }
        return result;
    };
};

For convenience you can also attach compose to Function.prototype so it can be called on any function:

Function.prototype.compose = function(prevFunc){
    var nextFunc = this;
    return function(){
        return nextFunc.call(this, prevFunc.apply(this, arguments));
    };
};

Libraries such as lodash and ramda provide similar compose utilities; the key idea is the composition pattern rather than the specific implementation.

Using the compose function, we can combine simple unary functions. For example:

var toUpperCase = function(x){
    return x.toUpperCase();
};
var exclaim = function(x){
    return x + '!';
};
var print = compose(exclaim, toUpperCase);
print("wechat"); // => "WECHAT!"

Notice that each argument to compose is a single‑parameter function, echoing the currying concept discussed earlier.

2 Pure Function and the Advantages of Code Composition

A function whose return value depends solely on its input parameters and that does not modify external state is called a Pure Function . Treating each step of a data‑processing pipeline as a pure, curried function allows us to connect them with composition, producing a new function that also accepts a single argument.

Applying currying and composition together yields several benefits:

Highly modular code with strong reusability.

Pure functions are easy to test.

Since pure functions do not rely on external variables, maintenance costs are low.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaScriptfunction compositionCurryingPure Functions
WeChatFE
Written by

WeChatFE

Tencent WeChat Public Platform Frontend Team

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.