Hey.

Question on jakery. Why do jakery need the iterator methods each () and map () , which go over all DOM objects from the sample (the sample is a jakewer object) and do something (what is specified in the parameter function)? In jakery, and so full of specific function-methods (addClass (), css () ...), each of which goes over ALL objects from the sample and does something (add / remove style properties, classes, run around the DOM tree. ..), their possibilities, in theory, are enough to make EVERYTHING you want.

    1 answer 1

    in theory, enough to do everything you want.

    No, not enough. There are always special cases that are not provided in the built-in tools.

    Example: due to the absence of the reduce method with the help of each, you can select a part of the values ​​that are suitable for the condition.

    var positive = []; $('selector').each(function(i,el){ if(el.value > 0) positive.push({[i]:el}); }); 

    Or another option is to perform an action with a third-party object.

     $('span').each(function(i, el) { $('#res').append(el.innerHTML ? `~${el.innerHTML}~` : 'empty'); }) 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <span>1</span> <span>2</span> <span></span> <span>4</span> <span></span> <div id="res"></div> 

    In addition, you should not forget that the each method can be applied not only to jQuery objects, but also to ordinary objects, replacing for..in and arrays, unifying access, regardless of the type of collection.

    In addition, all of the listed methods use the each method within themselves.

    map - stands somewhat apart from all of this, since it is not intended to perform some function on the elements of a collection, but to get a new collection based on a given one.

    • that is, there was originally a each method, and then a bunch of other private methods (css, addClass ...) were thought up? - Dimon
    • one
      "there are always special cases that are not provided in the built-in tools" - can you give a simple example? - Dimon
    • @Dimon, added an example - Grundy