In object oriented programming, chaining means that some functions will return a reference to the object instance, allowing another call to a function of that object.
Wait, what?
An example would probably make more sense.
Suppose we have an object/class that has two functions, foo and bar. If those functions return a reference to the original object then these functions could be chained together, like so:
var object = new MyObject();
object.foo().bar(); //or we could do longer chains
Observe, this means that you don't have to do
var object = new MyObject();
object.foo();
object.bar();
Of course you can still do that but the chained version is a little more succinct and a little cooler. It's like how in natural languages one can say a few things within one sentence rather than stopping after every proposition.
A slightly more imaginative example.
If you imagine that the original class was some kind of collection, like an array or list then you could do things like:
function level3person(person)
{
return (person.level > 3);
}
var people = new e2UserCollection();
people
.find("e2users") //get all e2 people
.filter(level3person) //filter the ones above level 3
.addXP(100) //give them 100 xp
.each(function(person) { person.msg("You gained 100 XP"); }); //now tell them about it
The above was created in not-so-pseudo Javascript however you can do it in C++ and I'd say pretty much any object oriented language.
How to do it?
Basically just return a reference back to the object instance as the last line of appropriate function. In Javascript, you would do something like this :
e2UserCollection.prototype = {
//...obviously other code is here
find : function(whotofind) {
//code you'd put to find people
return this; //the magical part that sets up the chain
}
//lather, rinse, repeat for the other functions
}
If you were doing this in C++ you would do something like:
class Foo {
public:
//constructors + stuff
Foo& chaining_function();
};
Foo& Foo::chaining_function()
{
//code
return *this; //as usual, it's uglier in C++
}
Typical candidates for this is when your class has functions that don't return anything otherwise (i.e. they don't compute something and return it).
Also just remember not to over do it. Like natural languages, it's okay to stop to take a breath every now and then.
Update : raincomplex informs me that this isn't always possible. Specifically
In Python, there are cases in the standard library where chaining is explicitly disallowed in order to prevent confusion as to whether the (modified) instance returned is the same as or a copy of the instance on which the method was called (examples: list.sort() and list.reverse()---they return None)