Having studied a DSL for a while, and discovered so many interesting things, JavaScript uses most of the things that are chained to the call (method chaining). The interesting thing is Martin Flower points out:
Copy Code code as follows:
I ' ve also noticed a common misconception-many people seem to equate the fluent interfaces with method chaining. Certainly chaining is a common technique to use with fluent interfaces, but true fluency are much more than that.
Many people equate a chained call with a fluent interface. However, chained call is a common method of fluent interface, the real fluent interface is not only so little.
DSL Fluent Interface
The original intention of the fluent interface is to build a readable API, after all, the code is written to people.
Similarly, simply look at the earlier we were manipulating the DOM through method Cascade
Copy Code code as follows:
var btn = document.createelement ("button"); Create a <button> element
var t = document.createTextNode ("click ME"); Create a text node
Btn.appendchild (t); Append the text to <button>
Document.body.appendChild (BTN); Append <button> to <body>
And in the case of jquery, that's it.
Copy Code code as follows:
$ (' <span> '). Append ("click ME");
Wait a minute
So back we can create a simple example to show the simplest DSL
Copy Code code as follows:
Func = (function () {
This.add = function () {
Console.log (' 1 ');
return this;
};
This.result = function () {
Console.log (' 2 ');
return this;
};
return this;
});
var func = new Func ();
Func.add (). result ();
This, however, looks like an expression builder.
DSL Expression Builder
The Expression Builder object provides a coherent set of interfaces and then converts a coherent interface call to a call to the underlying command-query API.
Such an API, we can see in some of the APIs on the database:
Copy Code code as follows:
var query =
SQL (' Select name, desc from widgets ')
. WHERE (' Price < ', $ (params.max_price), and,
' Clearance = ', $ (params.clearance))
. By (' name ASC ');
Chain call There is a problem is the end of the code above, we do not end, it is very confusing. Adding a query and end seems to be a good result.
Other
Method Cascade
expressed as follows:
Copy Code code as follows: