Java Reference
In-Depth Information
Chaining Functions
If a method returns this , its methods can be chained together to form a sequence of method
calls that are called one after the other. For example, the superman object can call all three
of the super-power methods that were mixed in earlier at once:;
<< "Up, up and away! Superman soars through the air!"
"Superman can move faster than a speeding bullet!"
"Superman can see right through you!"
This is a technique that is commonly used by a number of JavaScript libraries, most notably
jQuery. It helps to make code more concise by keeping multiple method calls on the same
line, and with some clever method naming it can make the calls read almost like a sentence;
the Jasmine testing library makes use of this.
A big drawback with this technique is that it can make code more difficult to debug. If an
error is reported as occurring on a particular line, there is no way of knowing which method
caused the error, since there are multiple method calls on that line.
It's worth keeping in mind that if a method lacks a meaningful return value, it might as well
return this so that chaining is possible.
Search WWH ::

Custom Search