From Newsgroup: comp.lang.javascript
Looks like an instance of method chaining style
https://en.wikipedia.org/wiki/Method_chaining
It is heralded as something powerful:
Method chaining is a powerful programming pattern that allows you to
call multiple methods on an object in a single line of code. It enhances
code readability and conciseness by eliminating the need for
intermediate variables or repeated method calls. In this blog post,
we'll explore method chaining in Python, explain it using a simple
example, discuss its use cases, and conclude with its benefits.
https://nikhilakki.in/understanding-method-chaining-in-python
But I have my doubts. Its also related to so called
Fluent Interfaces. When you design APIs so that they
support method chaining:
https://en.wikipedia.org/wiki/Fluent_interface
So called builders often exhibit a fluent interfrace. I
recently had a revelation, that many builders
are rather cheaters, for example I thought the appropriate
thing to do would be:
builder = builder.header(key, value);
So the fluent interface would give me a new version of the
build, with each method chaining call. Just like the replaceAll
gives a new string. But this is often not the case,
it would require that all headers are copied somehow. So we
find in the implementation of header() that it just returns
this, and the method chaining works with a side effect:
public HttpRequestBuilderImpl header(String name, String value) {
checkNameAndValue(name, value);
headersBuilder.addHeader(name, value);
return this;
}
So method chaining might not always satisfied the same expectations
about being a more "functional" approach.
Lawrence D'Oliveiro schrieb:
Is this valid code?
function escape_html(s)
{
return s
.replaceAll("&", "&") /* always do first, rest can be in any order */
.replaceAll("\"", """)
.replaceAll("<", "<")
.replaceAll(">", ">")
.replaceAll("\t", "	")
.replaceAll("\n", " ")
} /*escape_html*/
--- Synchronet 3.20a-Linux NewsLink 1.114