The core idea of jquery is write Less,do more (write less, do more), then the chain method design coincides with this core idea. Then the deep thinking of this design is actually a kind of internal DSL.
DSL refers to domain specific Language, which is the language used to describe and solve specific domain problems
We post a piece of code:
$ (' input[type= "button"] ") . EQ (0). Click (function() { alert (' click me! ') );}). End (). EQ (1). Click (function() { $ (' input[type= ' button ']:eq (0) '). Trigger (' Click ');}). End (). EQ (2). Toggle (function() { $ ('. AA '). Hide (' Slow ' function () { $ ('. AA '). Show (' Slow ');});
Looking at the structure of this code, we can more or less guess its meaning:
L find the INPUT element of type button
L Find the first button and bind the Click event handler function
L return all buttons and find the second one.
L BIND the Click event handler for the second button
L BIND the Toggle event handler for a third button
So the benefits of jquery's internal DSL form are visible-when you write code, the code is closer to the author's thinking pattern, and when you read the code, it makes it easier for the reader to understand the meaning of the code.
The application of DSL can effectively improve the maintainability of the system (reduce the distance between the implementation model and the domain model, improve the readability of the realization) and flexibility, and provide the efficiency of the development.