In JavaScript, the shadow of Enumerable is not found at all, because this part was borrowed by the Prototype author from Ruby. In fact, Enumerable has no chance to be directly applied. It is mixed into other objects. It can be said to be a "parent class" of other objects (but it just calls the extend method of Object and directly copies the method).
I'm not familiar with Ruby, but looking at some methods in Enumerable is somewhat similar to those in Python.
One of the most important methods of Enumerable is each. Each method should be familiar with it. Its function is to traverse all elements of a sequence and perform corresponding processing. However, most of them are applied to arrays, such as the forEach method of native arrays and chain calls in jQuery, all rely on each method. Because the jQuery selector returns an array of DOM objects, and then calls each on the returned array, thus processing each element separately.
Generally, each has two parameters: one is the context corresponding to the functions and methods that are processed by iteratively.
var each = || function(iterator,context){
for(var i = 0,len = ; i < len ; i++){
(context,this[i],this);
}
};
Following the above method, we extend an Array object to print all the current elements.
= || function(iterator,context){
for(var i = 0,len = ; i < len ; i++){
(context,this[i],i,this);
}
};
= function(){
(function(item){
(item);
});
}
([1,2,3,4].print());//1,2,3,4
In Enumerable, each does not correspond to a specific method. As mentioned earlier, Enumerable is not applied at the same time, but is applied to other objects as a "parent class". Therefore, its each method is to call the "subclass" _each method. Therefore, any object mixed into the Enumerable module must provide a _each method as iterative code acting on the actual loop.
Now implement a _each method and a each method, implement one:
= function(iterator,context){
this._each(iterator,context)
}
._each = function(iterator,context){
for(var i = 0,len = ; i < len ; i++){
(context,this[i],i,this);
}
};
As mentioned earlier, _each only needs to provide an iterator parameter, but since _each is also extended to the above, the context parameter is also attached when implementing it. Therefore, in Enumerable, the second context parameter of _each is not used, and whether it is implemented has no effect on each. Therefore, the above implementation should not depend on the context of _each, so modify each as follows:
= function(iterator,context){
var index = 0;
this._each(function(value){
(context,value,index++);
})
}
In this way, the independence of each method is improved, and this Enumerable can also be used in subsequent Hash. Any object that looks at traversal can obtain the corresponding method from Enumerable as long as the _each method is provided.
Therefore, if the above print example is implemented in the form of Enumerable, we will get the following result:
var Enumerable = {};
= function(iterator, context) {
var index = 0;
this._each(function(value){
(context, value, index++);
});
return this;
};
= function(){
(function(item){
(item);
})
};
._each = function(iterator,context){
for(var i = 0,len = ; i < len ; i++){
(context,this[i],i,this);
}
};
//The extend method is used in the following implementation source code
for(var key in Enumerable){
[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. Array and Hash are mixed with the Enumerable object, which is quite important.
Please indicate the source of Xiaoxishanzi【/xesam/】
I'm not familiar with Ruby, but looking at some methods in Enumerable is somewhat similar to those in Python.
One of the most important methods of Enumerable is each. Each method should be familiar with it. Its function is to traverse all elements of a sequence and perform corresponding processing. However, most of them are applied to arrays, such as the forEach method of native arrays and chain calls in jQuery, all rely on each method. Because the jQuery selector returns an array of DOM objects, and then calls each on the returned array, thus processing each element separately.
Generally, each has two parameters: one is the context corresponding to the functions and methods that are processed by iteratively.
Copy the codeThe code is as follows:
var each = || function(iterator,context){
for(var i = 0,len = ; i < len ; i++){
(context,this[i],this);
}
};
Following the above method, we extend an Array object to print all the current elements.
Copy the codeThe code is as follows:
= || function(iterator,context){
for(var i = 0,len = ; i < len ; i++){
(context,this[i],i,this);
}
};
= function(){
(function(item){
(item);
});
}
([1,2,3,4].print());//1,2,3,4
In Enumerable, each does not correspond to a specific method. As mentioned earlier, Enumerable is not applied at the same time, but is applied to other objects as a "parent class". Therefore, its each method is to call the "subclass" _each method. Therefore, any object mixed into the Enumerable module must provide a _each method as iterative code acting on the actual loop.
Now implement a _each method and a each method, implement one:
Copy the codeThe code is as follows:
= function(iterator,context){
this._each(iterator,context)
}
._each = function(iterator,context){
for(var i = 0,len = ; i < len ; i++){
(context,this[i],i,this);
}
};
As mentioned earlier, _each only needs to provide an iterator parameter, but since _each is also extended to the above, the context parameter is also attached when implementing it. Therefore, in Enumerable, the second context parameter of _each is not used, and whether it is implemented has no effect on each. Therefore, the above implementation should not depend on the context of _each, so modify each as follows:
Copy the codeThe code is as follows:
= function(iterator,context){
var index = 0;
this._each(function(value){
(context,value,index++);
})
}
In this way, the independence of each method is improved, and this Enumerable can also be used in subsequent Hash. Any object that looks at traversal can obtain the corresponding method from Enumerable as long as the _each method is provided.
Therefore, if the above print example is implemented in the form of Enumerable, we will get the following result:
Copy the codeThe code is as follows:
var Enumerable = {};
= function(iterator, context) {
var index = 0;
this._each(function(value){
(context, value, index++);
});
return this;
};
= function(){
(function(item){
(item);
})
};
._each = function(iterator,context){
for(var i = 0,len = ; i < len ; i++){
(context,this[i],i,this);
}
};
//The extend method is used in the following implementation source code
for(var key in Enumerable){
[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. Array and Hash are mixed with the Enumerable object, which is quite important.
Please indicate the source of Xiaoxishanzi【/xesam/】