Analysis of jquery's chain call-based each function

Source: Internet
Author: User
Tags jquery library


If you are not clear about jquery's $ () packer function, please refer to my previous log:Analysis of jquery's chain call

Back then, although jquery has brought more and more front-end technologies to be learned (I just happened to have a basic jquery tutorial in the school library (II) and started to learn the front-end technology in depth ), jquery's blog posts are even more than JavaScript, which greatly lowers the programming threshold, but it hides too many details, such as $ ('# id '). append ('<p> XXX </P> 'privacy .clone().appendto(x).end().css (...)................ in this mode, it is difficult to find the shadow of conventional JavaScript. The differences between browsers seem to disappear. I don't think most of them can go back to thinking about how JavaScript works in different browsers in this comfortable environment, it's hard to say that happiness is a curse.

Let's get down to the truth...

Because the $ () function returns an object that contains the native DOM object array, and the extended functions on this object prototype are all used to operate the native DOM object, the circular traversal operation is indispensable, anyone familiar with the jquery library knows that there is a jquery. the each () function is used by most functions involving jquery objects: simple implementationIt should beAs shown in the following figure:

(I reiterate that it is only a simple implementation principle and does not consider specific functional issues)



 If (! Window ['$'])
Window ['$'] = Window ['jquery '];
/* The above area is the content in the closure of the previous article.
* Defines jquery. Each to perform operations based on input objects.
* @ Param {object} OBJ
* @ Param {object} func
* For simplicity, I only considered arrays and jquery objects. Like in the previous article, the principle should be consistent.
Window ['jquery '] ['Each'] = function (OBJ, func ){
If (obj. constructor = array ){
For (VAR I = 0; I <obj. length; I ++ ){
Func. call (OBJ [I], I, OBJ [I]); // you can see that the input func should be in the form of function (I, item) I, which indicates the subscript to be traversed, item indicates the objects traversed in the array.
} Else if (obj. Elements & obj. elements. constructor = array) {// The legendaryDuck ruleInstead of determining whether it is a jquery instance, I will operate on you as long as you have array-type elements.
For (VAR I = 0; I <obj. elements. length; I ++ ){
Func. call (obj. elements [I], I, obj. elements [I]); // you can see that the passed func is like function (I, item) I, which indicates the subscript to be traversed, and item indicates the object to be traversed in the array.
} Else {
Return NULL;


Based on this function, you can start to expand the _ jquery prototype. First, you need to write a method that can be directly called by the wrapper object.Each(This is not repeated). Then, you can call this each function to traverse the object array,

For example:



 // Write in the closure. Note that the name of the jquery constructor conflicting with the name of yesterday is changed to _ jquery.
_ Jquery. Prototype = {
Each: function (func ){
Jquery. Each (this, func );
Return this;
ATTR: function (Key, value ){
// The example defines the operation attribute function of this set and get.

If (arguments. Length = 0 ){
Return NULL;
If (arguments. Length = 1 ){
Return this. elements [0]. getattribute (key );
Else if (arguments. Length = 2 ){
This. Each (function (I, item ){
// Here we can see the benefits of redefining the each method 1: simplified code, 2: In the internal function, this still points to the called wrapper object rather than the window
Item. setattribute (Key, value );

* Other methods can be introduced here.




Next we will do a few simple tests :(Or the previous test html)


     VaR K = $ ('# header ');
Using LES. Write (K. ATTR ('title', 'test title! '). ATTR ('title'); // chained call


Test title!

The each method can be used to effectively expand the package.

There are three main points that affect jquery's chain call. after the fact, it is not that simple, and the maintenance of jquery's internal code is not as good as some libraries, although it is very easy to use at least in terms of operations (of course, it is only for some small operations, big projects cannot be reached at the moment, it is not easy to follow a lot of people and clouds ). however, even from the perspective of the traversal operation, we can also see that this library can only rely on the refined plug-in, and the expansion will only look bloated.

Note: Anyone who has carefully analyzed the jquery source code will surely sneer at the so-called poor implementation, I have read just a few good books, such as Javascript DOM advanced programming and JavaScript advanced programming design patterns, and I have been touched by them, which may be quite different from the implementation of jquery, if yes, I hope I can correct it.

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: 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.