HN user

ookookook

3 karma
Posts0
Comments1
View on HN
No posts found.

I like to extract out non-semantic parts of a function and wrap the main functionality with these parts. So, if I wanted to create a function that could be called in a fluent style I could write :

    function doSomething() {
       // do stuff
       return this;
    }
However, the `return this` line doesn't really have anything to do with the core of what the method is supposed to do. Its largely an implementation detail.

I feel the method looks much better like :

    var doSomething = fluent(function() {
      // doSomething
    });
The fluent function handles the `return this;` so it doesn't obscure the core method.

It is one of most beautiful aspects of javascript that it enables you to build functions from other functions.