jQuery缓存$(this)是否有意义? [英] Does it make sense to jQuery cache $(this)?

查看:75
本文介绍了jQuery缓存$(this)是否有意义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍在学习有关jQuery的知识,但是我找不到一个可靠的答案.我知道每次使用jQuery选择器$(...)都会有性能损失,但是$(this)是否有相当大的代价要在大量使用它之前对其进行缓存?

I'm still learning about jQuery, but I have not been able to find a solid answer. I know every time you use the jQuery selector $(...) there is a performance cost, but does $(this) have a significant cost where you should cache it before using it a lot?

var $this = $(this);

推荐答案

如果您经常使用它,是的,这很有意义,也可以说是连锁的,例如:

If you're using it a lot, yes it makes sense, or chain, e.g.:

$(this).fadeIn().fadeOut().somethingElse();

是否昂贵?并非并非如此,但是如果您处于一个循环中,成本就会成倍增加,因此最好对其进行缓存.如果您不止一次地使用它,那么多少意义取决于我认为那段代码对性能的影响.

If it expensive? No not really in the grand scheme of things...but if you're in a loop the cost multiplies so it's best to cache it. If you're using it more than once it makes sense, how much sense depends on how much performance matters for that bit of code I suppose.

这篇关于jQuery缓存$(this)是否有意义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆