After studying DSL carefully for a while, I discovered several interesting things. One of the most used things in JavaScript is probably chained calls (Method Chaining). Interestingly, Martin Flower pointed out:
I've also noticed a common misconception - many people seem to equate fluent interfaces with Method Chaining. Certainly chaining is a common technique to use with fluent interfaces, but true fluency is much more than that.
Many people equate chained calls with fluent interfaces. However, chain call is a common method of fluent interface, and the real fluent interface is more than that.
DSL smooth interface
The original intention of fluent interface is to build a readable API. After all, the code is written for people to read.
Similarly, let’s take a brief look at how we manipulated the DOM through method cascading earlier
The expression generator object provides a set of coherent interfaces, and then converts the coherent interface calls into calls to the underlying command-query API.
Such an API can be seen in some APIs about databases:
var query =
SQL('select name, desc from widgets')
.WHERE('price < ', $(params.max_price), AND,
'clearance = ', $(params.clearance))
.ORDERBY('name asc');
One problem with chained calls is the ending. In the code above, we don’t have the ending, which is very confusing. . Adding a query and end seems to be a good result.
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn