As OP noted, there is no statement after the while condition. Pay attention to the semicolon after the while loop, return this only starts after the while loop completes, and this means that any of the three statements evaluates to false. If all you want to know is how the while loop works, Philip Bartuzi's answer is probably the most comprehensive. I will try to give a broader answer about what each2 method each2 .
As noted in the docs, this is a more efficient version of jQuery #each or Array.prototype.forEach or Underline #each or lodash #forEach , which is specifically designed to use select2. It is used to iterate over any array or other iterable wrapped in a jQuery object. Therefore, it is used for string arrays, as well as for jQuery collections.
How it works, the ( this ) area is the array on which it was called. He is given a function as an argument. This is the name of the other methods each , which I mentioned earlier. The function provided by each2 is called once for each element of the array. The while loop is a kind of hacker way of iterating over each element of the array and calling the function, setting the element as context and passing the index in the array, and the element as a jQuery object as the first and second arguments. Each statement in the condition of the while must be evaluated to determine if it is true, and this process is used to actually assign values ββto variables and increment i . The c.call(j[0], i, j) !== false allows the function to break the loop earlier, returning false. If the function returns false, the while loop will stop, otherwise it will continue until i is greater than l , which means that each element of the array is used. Returning this after this simply allows you to bind another method to the array after .each2 .
In principle, the while can be rewritten to:
var j = $([0]), i = 0, l = this.length, continue = true; while (i < l) { i++; j.context = j[0] = this[i]; continue = c.call(j[0], i, j); if (!continue) { break; } }
but there are probably some performance reasons that cannot be optimized by the browser.
It is more fragile than the usual each methods. For example, if the element in the array is false, for example 0 , (j.context = j[0] = this[i]) will be false, which will end the loop. But it is used only in specialized cases of select2 code, where this should not be.
Skip some examples.
function syncCssClasses(dest, src, adapter) { var classes, replacements = [], adapted; classes = $.trim(dest.attr("class")); if (classes) { classes = '' + classes; // for IE which returns object $(classes.split(/\s+/)).each2(function() { if (this.indexOf("select2-") === 0) { replacements.push(this); } }); } ...
^ This code gets classes from the dest DOM element. Classes are divided into an array of strings (the string is divided into space characters, which is the regular expression \s+ ), each class name is an element in the array. There is no special jQuery work here, so 2 arguments are not used, which is the function called by each2 . If the class begins with 'select2-', the class is added to the array named replacements . The "select2-" classes are copied, quite simply.
group.children=[]; $(datum.children).each2(function(i, childDatum) { process(childDatum, group.children); });
^ For brevity, I have not included the rest of this method, but it is part of the process method. In this case, each2 used for the recursive process a node and all its children. $(datum.children) is a jQuery choice, each "child" (named childDatum ) will be processed one at a time, and children will go through the same process. The array group.children will be used as a collection, everything that is added to this array for each childDatum will be available, so after each2 it will contain everything that was added during processing of all children, grandchildren, etc.