JQuery API design

Source: Internet
Author: User
JQuery will release the official version 1.4 immediately. The Code will also be migrated from googlecode to github. jQuery is the first JS class library I have come into contact with. As the saying goes, first love is always hard to forget. A year ago, this hard to forget was just a kind of pure feeling, and I couldn't tell the specific reason. I re-read the source code on github a few days ago. In terms of pure functionality, jQue jQuery will immediately release the official version 1.4, and the code will be migrated from googlecode to github. jQuery is the first JS class library I have come into contact with. As the saying goes, it is always hard to forget my first love. A year ago, this hard to forget was just a kind of pure feeling, and I couldn't tell the specific reason. I re-read the source code on github a few days ago. In terms of functionality alone, jQuery is not particularly outstanding. What makes me so reluctant?

When I set up taskspeed yesterday and checked jQuery's test code, I suddenly learned a secret that everyone may already know:

JQuery's best and most frustrating is its API design.
For example, the dom-style api, YUI3, and MooTools frameworks adopt the traditional method:

El. setStyle (prop, val); el. getStyle (prop); el. setStyles ({propA: valA, propB: valB}); el. getStyles (propA, propB); // supported by MooTools

In jQuery, a css method is all done:

El.css (prop); // getStyleel.css (prop, val); // setStyleel.css ({propA: valA, propB, valB}); // setStylesel.css (prop, func); // func is a function that returns the val value.

Compared with the above two API designs, jQuery seems less "standard" at first glance ". But in terms of memory and flexibility, I think jQuery's design is more user-friendly. JQuery's API is also in line with the progressive thinking of learning: first, let's look at the simplest form of el.css (prop). Then we can find that there are two parameters, and then we can find that the parameter can be map, we can further find that val can also be a function. The func parameter can even surprise learners: it can be used like this! JQuery brings a kind of progress and pleasure into the process of learning and using. It is really beautiful!

YUI3 APIs lack such fun. When querying jQuery APIs, you will find a way to find secrets. YUI's document query makes people feel like a job, a bit boring.

Compared with YUI2, YUI3's API has made some changes. In YUI2, the method name of YAHOO. util. Dom is strictly started with a verb. Although some methods have long names, the general regularity is strong and the memory is good. In YUI3, byId, elementByAxis, and other naming methods are displayed. Just to save a few characters? This inconsistency is confusing. There are also some naming methods: ancestor and docHeight. At first glance, it is difficult to use this method.

My wife said that she would not need to enter the code if she was going to bed. Finally, YUI3's API is quite good. For example, it is very rigorous to Name Node methods. Ancestor is also used to name next and prev. That is to say, Y. Dom has actually become an internal API and users are not encouraged to call it directly.

But somehow, I still think that jQuery's API design is higher than one level. The following is a popular saying:
JQuery API provides a better user experience.

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.