admin管理员组文章数量:1389750
I see a lot of jQuery examples that do something like
var $element = $('#element');
$element.foo...
rather than just typing
$('#element').foo...
and I do realize there is a small bit of typing saved if you are working with $element a lot, but what about those times that $element is only called once or twice? Why do some developers declare it as a jQuery object variable in those instances? Is it also more efficient for the browser to process?
I see a lot of jQuery examples that do something like
var $element = $('#element');
$element.foo...
rather than just typing
$('#element').foo...
and I do realize there is a small bit of typing saved if you are working with $element a lot, but what about those times that $element is only called once or twice? Why do some developers declare it as a jQuery object variable in those instances? Is it also more efficient for the browser to process?
Share Improve this question edited Apr 4, 2014 at 21:08 Jonathan E. Landrum asked Apr 4, 2014 at 20:58 Jonathan E. LandrumJonathan E. Landrum 3,1824 gold badges36 silver badges49 bronze badges 17- 2 If you're only re-using it once or twice, then no, there's no real improvement at all. – j08691 Commented Apr 4, 2014 at 21:00
- 2 You save maybe 6-7 bytes everytime it's minified, and that's something. – adeneo Commented Apr 4, 2014 at 21:01
- 4 Why do they do it when it's only used once? Who knows. You'd have to ask the developer. Why when only twice? It's a judgement call, and depends on the situation. Is the code in a loop? In a rapidly invoked event handler? – cookie monster Commented Apr 4, 2014 at 21:01
- 3 @cookiemonster - while it is a judgment call, re-querying the DOM for the same object twice is not a good judgment. Caching it when it is only used once serves no benefit, but caching it on multiple uses (even as little as two) is always more efficient because it prevents the DOM from being queried multiple times for the same object. – PlantTheIdea Commented Apr 4, 2014 at 21:03
- 2 @adeneo - "micro optimization is usually avoided" ... you and i e from very, very different perspectives then. – PlantTheIdea Commented Apr 4, 2014 at 21:38
2 Answers
Reset to default 10Usually this is done to avoid either re-wrapping an element or re-querying the page for the selector.
Such as in a click event
$('.className').click(function(){
var $this = $(this);
callSomeHelper($this);
$this.hide();
if( $this.data("placement") == "somePlacement" ){
//some logic
}
});
The real saver is when it is referencing a set of elements.
var $allSides = $('.side');
And now back in the click event, you can reference this without having to re-query
$('.top').click(function(){
var door = $allSides.find(':visible');
});
There are obviously more in depth examples, but these two are the main cases that the variable is stored.
It prevents from overwriting variable from another script
本文标签: javascriptWhy Create jQuery Object VariablesStack Overflow
版权声明:本文标题:javascript - Why Create jQuery Object Variables? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744598205a2614906.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论