JavaScript Object Method Chain: Useful?

So ... messing with JavaScript with an idea that is new to me, with the Object methods returns the Object object of which they are a part; this leads to adhesion. Then my question is: how can this be useful? I threw this together to check out the main works:

<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> 

This is clearly not a malevolently efficient instance in which to use this concept, so that you could tell me what does it right (except for jQuery, please)?

+18
javascript object method-chaining
Mar 02 '09 at 18:48
source share
9 answers

Well, here is an example of a not-so-real world, but I think you will understand this idea. If it allows you to perform several different operations on the 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(); 
+23
Mar 02 '09 at 18:57
source share

For a completely different (non-OO) example, the chain is somewhat similar to Unix pipelines . Each step of the Unix channel returns full (modified) content suitable for sending to the next step:

 cat file1 file2 | sort -n | awk '{print $2}' | sed 's/@/ at /g' 
+8
Mar 02 '09 at 19:05
source share

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

Yes, I think it can be very useful, but like any design template, it should be used only when necessary.

Edit: here is the twi api client in C # using a free interface - http://code.google.com/p/tweetsharp/

+7
Mar 02 '09 at 19:00
source share

One example where this is useful, with a slight change to your problem - instead of returning the same object, you create the object immutable. Then your functions will return a new instance of the same type, but with properties already set.

It has many practical applications, especially in the field of functional programming.

+6
Mar 02 '09 at 19:06
source share

While it doesn’t work like your example (TBH, I never saw this done before), jquery believes that the β€œchain” is very useful , and jquery is pretty much a measure these days when it comes to JS web frameworks ... so yes :-)

+3
Mar 02 '09 at 19:42
source share

I found this question, looking for a general solution on how to make methods integer after they are defined. Here is what I came up with. I am a neophyte of JavaScript; Buyer beware.

 makeChainable = function() { var receiver = arguments[0] for (var i = 1; i < arguments.length; i++) { functionName = arguments[i]; (function() { wrapped = receiver[functionName]; receiver[functionName] = function() { wrapped.apply(receiver, arguments); return receiver; } })(); } } daisy = { name: 'Daisy', moo: function() { console.log(this.name + " moos!") } } makeChainable(daisy, 'moo'); daisy.moo().moo().moo(); 
+3
Apr 03 '13 at
source share

In JavaScript, all this time appears when navigating the DOM. In particular, when you try to wade through a bunch of elements that do not have identifiers.

For example, a question arose about SO regarding finding the first element of a table . It can include many loops or a chain of commands.

+1
Mar 02 '09 at 19:16
source share

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

0
Jul 08 '09 at 18:35
source share

All children love chains. However, in my experience, it should be used with caution, as it can reduce the readability of the code. In other words, do what makes sense to you, and is easy to understand for other programmers who are familiar with the concept.

-2
Apr 08 '09 at 0:37
source share



All Articles