O uso de $ this em vez de $ (this) fornece um aprimoramento de desempenho?
Assume Tenho o seguinte exemplo:
Exemplo Um
$('.my_Selector_Selected_More_Than_One_Element').each(function() {
$(this).stuff();
$(this).moreStuff();
$(this).otherStuff();
$(this).herStuff();
$(this).myStuff();
$(this).theirStuff();
$(this).children().each(function(){
howMuchStuff();
});
$(this).tooMuchStuff();
// Plus just some regular stuff
$(this).css('display','none');
$(this).css('font-weight','bold');
$(this).has('.hisBabiesStuff').css('color','light blue');
$(this).has('.herBabiesStuff').css('color','pink');
}
Agora, pode ser:
Exemplo Dois
$('.my_Selector_Selected_More_Than_One_Element').each(function() {
$this = $(this);
$this.stuff();
$this.moreStuff();
$this.otherStuff();
$this.herStuff();
$this.myStuff();
$this.theirStuff();
$this.children().each(function(){
howMuchStuff();
});
$this.tooMuchStuff();
// Plus just some regular stuff
$this.css('display','none');
$this.css('font-weight','bold');
$this.has('.hisBabiesStuff').css('color','light blue');
$this.has('.herBabiesStuff').css('color','pink');
}
O ponto não é o código real, mas o uso de$(this)
quando é usado mais de uma vez / duas vezes / três vezes ou mai
Estou melhor em termos de desempenho usandoexemplo dois do queexemplo um (talvez com uma explicação por que ou por que não
EDITAR NOT
Suspeito que dois sejam melhores; o que eu estava com um pouco de medo foi apimentar meu código com$this
e, sem querer, introduzir um erro potencialmente difícil de diagnosticar quando inevitavelmente esqueço de adicionar o$this
para um manipulador de eventos. Então, eu devo usarvar $this = $(this)
ou$this = $(this)
por esta
Obrigado
EDITA
omo Scott indica abaixo, isso é considerado cache no jQuer
http: //jquery-howto.blogspot.com/2008/12/caching-in-jquery.htm
Jared