7
When I learned to use the method .end
jQuery, I realized that it was a powerful tool that ensures a lot of expressiveness to the code (and I’ve already seen how to integrate it to my plugins). Although I have a basic knowledge of how it works:
$(meuSeletor) // Seleciona um conjunto de elemtentos [meuSeletor]
.fazAlgo() // Faz algo com ele, mantém o conjunto [meuSeletor]
.find(sub) // Acha um subconjunto, mas empilha o anterior [sub, meuSeletor]
.fazAlgo() // Faz algo com esse subconjunto [sub, meuSeletor]
.end() // "Desempilha": volta o que tinha antes [meuSeletor]
.fazAlgo(); // Faz algo com o conjunto original [meuSeletor]
Some functions like the andSelf
and the addBack
still intrigue me (I remember long ago I tried to use the andSelf
in practice, but did not have the expected result). I would like someone to explain, simply and succinctly, how this jQuery stacking system works, and how this can benefit us (in the authorship of plugins, for example).
The question is about the stacking system itself, or about addBack?
– bfavaretto
@bfavaretto It is about the stacking system itself (the
addBack
, I just saw here that it is similar to theaddSelf
- now obsolete). It’s easy to see in the API what each individual method does, but I lack the overview of the whole. I thought it would be interesting to ask here, instead of testing case by case everything that comes into my head (e.g., if I do it myselfaddBack
afterwardend
, what will happen? if I dopushStack
and in the original object I makeend
, what will happen? etc).– mgibsonbr