JavaScript DSL Fluent interface (using chained invocation) instance _javascript tips

Source: Internet
Author: User

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:

A.B ();
A.C ();

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.