jquery's internal DSL

Source: Internet
Author: User
Tags to domain

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.

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.