views:

1223

answers:

8

So... messing around in JavaScript with an idea that's new to me, having methods of an Object return the Object of which they are methods; this then leads to chainability. My question, then: how can this be useful? I threw this together to test the fundamental workings:

<script>
MathChain = function()
 {
    this.pass = function()
     {
     this.multiply = eval(arguments.join('*'));
     this.add = eval(arguments.join('+'));
     return this;
     }
 }

m = new MathChain().pass(5, 10, 20).multiply; // 1000
a = new MathChain().pass(5, 10, 20).add;      // 35
</script>

That's obviously not a viciously efficient instance in which one would use this concept, so could you point me to something that does do so properly (aside from jQuery, please)?

+4  A: 

Well, here is a not very real-world applicable example, but I think you'll get the idea. If allows you to do a number of different operations on an object, and provides convenience.

var truck = function() {

    this.turnLeft = function {

       // turn left
       return this;

    }

    this.turnRight = function {

       // turn right
       return this;

    }

    this.goReallyFast = function {

       // go fast!
       return this;

    }

};

// My get-away plan
var myTruck = new truck();
myTruck.turnLeft().turnRight().goReallyFast();
jonstjohn
Heh, I like it; it definitely demonstrates a potential use case very well. Um... no real-world usage then outside of how it's implemented in various libraries?
Hexagon Theory
Hmmm, I think I understand what you're getting at. Are you getting at the efficiency question? I've always seen it as a matter of convenience. I know some people think that chaining is less readable than having each method on its own line.
jonstjohn
+5  A: 

Fluent interface - http://en.wikipedia.org/wiki/Fluent_interface

Yea I think it could be very useful but like any design pattern should only be used when needed

Edit: here is twitter client api in c# using a fluent interface - http://code.google.com/p/tweetsharp/

BPAndrew
+3  A: 

For a very different (non-OO) example, chaining is somewhat analogous to Unix pipelines. Each step of a Unix pipe returns the full (modified) content, suitable for sending on to the next step:

cat file1 file2 | sort -n | awk '{print $2}' | sed 's/@/ at /g'
Anirvan
+1  A: 
Joel Coehoorn
Hm... would that be return this.clone(), then?
Hexagon Theory
um... sort of, but I'm not sure you understand that concept yet. It's okay: immutable objects aren't easy. Look into .Net's String class for good example to get started.
Joel Coehoorn
+1  A: 

In JavaScript this comes up all the time when navigating the DOM. In particular when trying to wade your way through a bunch of elements that don't have ids.

For example there was a question on SO regarding finding the first element of a table. It can involve a lot of loops or chained commands.

Paulo
+1  A: 

While it doesn't work in the same way as your example (TBH I've never seen it done that way before), jquery considers "chaining" to be very useful, and jquery is pretty much the yardstick these days when it comes to JS web frameworks... so yeah :-)

Orion Edwards
A: 

All the kids love chaining. However, in my experience it should be used with care since it can decrease the readability of the code. In other words, do what makes sense to you and can be easily understood by other programmers who have a basic familiarity with the concept..

Michael Luton
A: 

JavaScript chaining can be very useful if you want to preform a series of actions on a single object. I agree with Michael Luton below, chaining should be handled with care. If you add one or two chained methods onto an object that is still readable. If you start adding four, five, or even nine, then your code becomes harder not only to read but to maintain.

Lark