在jQuery中缓存$(this)是最佳实践吗? [英] Caching $(this) in jQuery is a best practice?
本文介绍了在jQuery中缓存$(this)是最佳实践吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我们都知道缓存对DOM的调用是很好的,所以与其多次调用$('#someElement'),不如将其保存到var $ someElement中并使用它.
We all know it's good to cache calls to the DOM, so instead of calling $('#someElement') more times, just save it to a var $someElement and use that.
但是,例如在事件侦听器中使用$(this)时是否相同? 应该缓存$(this)吗?
But is it the same when using $(this) inside an event listener for example? Should $(this) be cached?
谢谢.
推荐答案
每次调用$(this)
或$(selector)
都是创建新jQuery对象的函数调用...因此,如果您已经创建了一次它,缓存将节省调用函数来再次创建相同对象的时间
Each time you call $(this)
or $(selector)
it is a function call to create a new jQuery object... so if you have already created it once, caching will save calling a function to create the same object again
这篇关于在jQuery中缓存$(this)是最佳实践吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文