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 开发者_JAVA百科approach with this kind of programming (ie keep the state of the objects unaltered and return a clone of the modified object).
regards, Jeroen
The technique is generally called Method Chaining. In your example above is forms part of a Fluent Interface
It's called a Fluent Interface, see http://en.wikipedia.org/wiki/Fluent_interface
精彩评论