jQuery中$ .cache的目的是什么? [英] What is the purpose of $.cache in jQuery?

查看:101
本文介绍了jQuery中$ .cache的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到通过 .on()注册的事件处理程序保存在 $。cache 中。
我也看到事件处理程序也保存在 $(elem).data()中。

I see that the event handlers registered through .on() are held in $.cache. I also see that the event handlers are also held in $(elem).data().

保存在 $。cache 中的对象是指注册事件的DOM节点。当DOM节点被分离时,这将导致内存泄漏,这使得$ code> .off()调用是必需的。

The objects held in $.cache refer to the DOM nodes on which the events are registered. This leads to memory leak when the DOM nodes are detached, and this makes .off() calls mandatory.

我有一种情况,我不知道DOM节点(我附加到事件处理程序)何时被分离。虽然我可以在我的代码中保留对该DOM节点的引用,并调用 .off()进行清理,但似乎不太好,因为它不是直接知道当DOM节点被删除时。

I have a situation where I don't know when the DOM node (to which I attached the event handler) is being detached. While I can hold the reference to that DOM node in my code and call .off() to clean up, it doesn't seem nice, because it is not straightforward to know when the DOM node is being removed.

最好的方法是什么?

推荐答案


最好的方法是什么?

"What is the best way to do this?"

如果要使用jQuery,您必须使用其API来删除元素,而您必须使用正确的方法,否则,如你所说,你会有内存泄漏。

If you're going to use jQuery, you must use its API for removing elements, and you must use the proper methods, otherwise, as you stated, you'll have memory leaks.

如果你不知道什么时候DOM节点被删除,它造成泄漏,我认为这意味着你正在jQuery旁边使用另一个库。这不是一个好主意,因为这个原因。

If you don't know when the DOM node is being removed, and if it is causing a leak, I'd assume this means that you are using another library alongside jQuery. That's just not a good idea for this very reason.

你需要确保任何受jQuery影响的元素都被jQuery删除。还有一些数据存储在您没有明确设置的 $。cache 中。这意味着应该使用jQuery删除所有元素,而不只是您认为可能拥有数据的元素。

You need to ensure that any elements affected by jQuery are removed by jQuery. There's also some data stored in $.cache that you didn't explicitly set. This means that all elements should be removed with jQuery, instead of just those that you think may have data.


在jQuery中 $。cache 的目的是什么? em>

"What is the purpose of $.cache in jQuery?"

将处理程序和其他数据与元素相关联。数据和元素之间的链接基本上是存储在元素上的expando属性上的序列号。

To associate handlers and other data with elements. The link between the data and the elements is basically a serial number stored on an expando property on the element.

如果删除没有jQuery的元素,则 $。cache 是孤儿。

If you remove the element without jQuery, the associated data in $.cache is orphaned.

这种方法的目的是为了防止潜在的泄漏。不幸的是,它可能会造成更严重的泄漏。

The purpose of this approach was to prevent potential leaks. Unfortunately it potentially creates more severe leaks.

这篇关于jQuery中$ .cache的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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