Prototype source code analysis Enumerable each method _ prototype-js tutorial

Source: Internet
Author: User
In javascript, the shadow of Enumerable cannot be found at all, because the Prototype was referenced by the author from Ruby. In javascript, the shadow of Enumerable cannot be found at all, because the Prototype was referenced by the author from Ruby. In addition, Enumerable has no direct application opportunity in practice, and is mixed into other objects. It can be said that it is a "parent class" of other objects (but only called the extend method of the Object, directly copy the method ).

I am not familiar with Ruby, but some methods in Enumerable are somewhat similar to those in Python.

One of the most important methods of Enumerable is each. each should be familiar with this method. Its function is to traverse all elements of a sequence and process them accordingly. However, most of them are applied to arrays. For example, the native array forEach method and the chain call in jQuery depend on the each method. Because the jQuery selector returns an array of DOM objects, and then calls each on the returned array to process each element separately.

Generally, each has two parameters: one is the context of the iteration function and method.

The Code is as follows:


Var each = Array. prototype. forEach | function (iterator, context ){
For (var I = 0, len = this. length; I <len; I ++ ){
Iterator. call (context, this [I], this );
}
};


According to the above method, we extend the print method for all elements of the current Array object.

The Code is as follows:


Array. prototype. each = Array. prototype. forEach | function (iterator, context ){
For (var I = 0, len = this. length; I <len; I ++ ){
Iterator. call (context, this [I], I, this );
}
};
Array. prototype. print = function (){
This. each (function (item ){
Console. log (item );
});
}
Console. log ([,]. print (); //,


In Enumerable, each does not correspond to a specific method. As mentioned earlier, Enumerable is applied as a "parent class" instead of an application, therefore, its each method calls the "subclass" _ each method. Therefore, any object mixed into the Enumerable module must provide a _ each method as an iterative code acting on the actual loop.

Now we can implement a _ each method and an each method on Array. prototype to implement 1:

The Code is as follows:


Array. prototype. each = function (iterator, context ){
This. _ each (iterator, context)
}
Array. prototype. _ each = function (iterator, context ){
For (var I = 0, len = this. length; I <len; I ++ ){
Iterator. call (context, this [I], I, this );
}
};


As mentioned earlier, _ each only needs to provide an iterator parameter. However, since _ each is also extended to Array. prototype, the context parameter is also included in the implementation. Therefore, in Enumerable, the second context parameter of _ each is not used, and whether implementation has no effect on each. Therefore, the preceding implementation should not depend on the context of _ each. Therefore, modify the each as follows:

The Code is as follows:


Array. prototype. each = function (iterator, context ){
Var index = 0;
This. _ each (function (value ){
Iterator. call (context, value, index ++ );
})
}


In this way, the independence of the each method is improved, and this Enumerable can be used in subsequent Hash. Any object that looks at the traversal can obtain the corresponding method from Enumerable as long as the _ each method is provided.

Therefore, the print example above is implemented in the form of Enumerable, and the following result is obtained:

The Code is as follows:


Var Enumerable = {};
Enumerable. each = function (iterator, context ){
Var index = 0;
This. _ each (function (value ){
Iterator. call (context, value, index ++ );
});
Return this;
};
Enumerable. print = function (){
This. each (function (item ){
Console. log (item );
})
};
Array. prototype. _ each = function (iterator, context ){
For (var I = 0, len = this. length; I <len; I ++ ){
Iterator. call (context, this [I], I, this );
}
};
// The following implementation source code uses the extend Method
For (var key in Enumerable ){
Array. prototype [key] = Enumerable [key];
};
[1, 2, 3, 4]. print (); // 1, 2, 3, 4


Understanding the implementation of each is the key to understanding the Enumerable object. The Array and Hash in the future are mixed into the Enumerable object, which is very important.
For more information, see http://www.cnblogs.com/xesam /]
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.