Zepto is a minimalist JavaScript library for modern browsers with a largely jQuery-compatible API
Method chaining history management for complex query operations. The stack module maintains a history of element collections during chaining operations, allowing developers to navigate back to previous selections.
Methods for navigating through the chain history during complex DOM operations.
/**
* End the most recent filtering operation and return to the previous set of elements
* @returns Previous collection in the chain
*/
$.fn.end();
/**
* Add the previous set of elements to the current collection
* @returns Combined collection including both current and previous elements
*/
$.fn.andSelf();Usage Examples:
// Using end() to navigate back in the chain
$('#container')
.find('.item')
.addClass('highlighted')
.end() // Back to #container
.addClass('processed');
// Complex chaining with multiple end() calls
$('.list')
.find('li')
.filter('.active')
.addClass('current')
.end() // Back to all li elements
.end() // Back to .list elements
.fadeIn();
// Using andSelf() to combine sets
$('.parent')
.children('.child')
.andSelf() // Include both .parent and .child elements
.addClass('combined');The stack operations modify the following methods to support chaining history:
filter() - Maintains reference to unfiltered collectionadd() - Maintains reference to original collectionnot() - Maintains reference to original collectioneq() - Maintains reference to full collectionfirst() - Maintains reference to full collectionlast() - Maintains reference to full collectionfind() - Maintains reference to search contextclosest() - Maintains reference to original collectionparents() - Maintains reference to original collectionparent() - Maintains reference to original collectionchildren() - Maintains reference to parent collectionsiblings() - Maintains reference to original collectionComplex operations combining multiple chain navigation techniques.
// Multi-level navigation example
$('#form')
.find('input')
.filter('[required]')
.addClass('required-field')
.end() // Back to all inputs
.filter('[type="email"]')
.addClass('email-field')
.end() // Back to all inputs
.end() // Back to #form
.find('button')
.addClass('form-button');
// Combining andSelf() with filtering
$('.menu')
.find('.menu-item')
.filter('.active')
.andSelf() // Include both active items and their parent menu
.addClass('highlight-group');
// Working with siblings and navigation
$('.selected')
.siblings()
.addClass('sibling')
.end() // Back to .selected elements
.addClass('original-selection');The stack maintains internal references to previous collections:
// Internal structure (for reference only)
{
length: 3,
0: element1,
1: element2,
2: element3,
prevObject: previousZeptoCollection, // Used by end()
context: originalContext // Original search context
}This internal structure allows end() to return to the prevObject and andSelf() to combine the current collection with prevObject.