dr jerry
dr jerry

Reputation: 10026

Is there a generic term for chained programming?

Most obvious example is jQuery where the result of a function is the modified object, to which you can apply same or different functions. An example below is from dan's blog, but examples are abundant over the internet.

var $inner = $("<div>inner</div>")
// append it to a new outer div
  .appendTo("<div>outer</div>")
// next change the jQuery chain to the "outer" div 
  .parent()
    // append the outer div to the body
    .appendTo("body")
// finally, go back to the last destructive command, 
// giving us back a pointer to the "inner" div
  .end();

Is there literature available on this technique? I'have only seen this being used in an imperative way (if you'd call $inner.append($moreInner) $inner is modified). Would it make sense to use an functional approach with this kind of programming (ie keep the state of the objects unaltered and return a clone of the modified object).

regards, Jeroen

Upvotes: 0

Views: 103

Answers (2)

bstick12
bstick12

Reputation: 1749

The technique is generally called Method Chaining. In your example above is forms part of a Fluent Interface

Upvotes: 3

phlogratos
phlogratos

Reputation: 13924

It's called a Fluent Interface, see http://en.wikipedia.org/wiki/Fluent_interface

Upvotes: 2

Related Questions