我应该用jQuery的数据()监视什么样的内存泄漏? [英] What sort of memory leaks should I watch for with jQuery's data()?

查看:145
本文介绍了我应该用jQuery的数据()监视什么样的内存泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该将每个数据()调用与以后的removeData()调用配对?



我的假设:jQuery的remove()将从DOM中删除元素,如果我没有任何其他引用要删除,我不必再做任何更多的清理。



但是,如果我有一些javascript var或对象引用要删除的元素之一,我需要清理它,我假定也适用于jQuery的数据函数,因为它以某种方式引用元素。



所以如果我需要在删除之前调用removeData,是否有一个快捷方式来删除与元素关联的所有数据,或者我必须通过名称显式调用? / p>

编辑:我浏览了源代码,并确认了Borgar和roosteronacid所说的。删除将元素从dom中删除并删除与它们一起存储的任何事件和数据 - 这是方便的,但让我想知道什么时候使用removeData()。可能不是经常的。

解决方案

jQuery的数据不保留对元素的引用,以便不需要担心内存泄漏。它的目的是解决这个确切的问题。



稍微简化它的工作原理:



将一个id成员添加到每个被触摸的DOM节点。涉及该DOM元素的所有后续操作都使用该id。

  var theNode = document.getElementById('examplenode'); 
theNode ['jQuery'+ timestamp] = someInternalNodeID;

您可以使用与jQuery相同的功能访问该ID:

  someInternalID = jQuery.data(document.body); 

当您将数据附加到节点时,它将存储在jQuery对象上的节点的内部ID 。您的 $(元素).data(key,value)内部翻译成如下:

 code> jQuery.cache [someInternalNodeID] [theKey] = theValue; 

所有内容都与相同的结构,包括事件处理程序:

  jQuery.cache [someInternalNodeID] ['events'] ['click'] = theHandler; 

当一个元素被删除时,jQuery可以抛弃所有的数据(和事件处理程序)一个简单的操作:

  delete jQuery.cache [someInternalNodeID]; 

理论上,您可能也会删除jQuery,而不会从任何引用发生泄漏。 jQuery甚至支持多个独立的库实例,每个实例都拥有它自己的一组数据或事件。



你可以看到John Resig在DOM是一个混乱演示


Should I pair every data() call with a later removeData() call?

My assumptions: jQuery's remove() will remove elements from the DOM, and if I don't have any other references to remove, I don't have to do any more clean up.

However, if I have some javascript var or object referring to one of the elements being removed, I'll need to clean that up, and I'm assuming that applies to jQuery's data function, too, because it's referencing the elements somehow.

So if I do need to call removeData before remove, is there a shortcut to remove all data associated with an element or do I have to call each explicitly by name?

Edit: I looked through the source code and confirmed what Borgar and roosteronacid said. Remove takes the elements out of the dom and deletes any events and data stored with them - which is convenient, but makes me wonder when you would use removeData(). Probably not often.

解决方案

jQuery's data does not keep a reference to the element so that you don't need to worry about memory leaks. Its intended purpose is to solve this exact problem.

A slight simplification of how it works:

An id member is added to each "touched" DOM node. All subsequent actions involving that DOM element use that id.

var theNode = document.getElementById('examplenode');
theNode[ 'jQuery' + timestamp ] = someInternalNodeID;

You can access the id using the same function jQuery uses:

someInternalID = jQuery.data( document.body );

When you append data to the node it stores that on the jQuery object, filed under the node's internal id. Your $(element).data(key,value) translates internally to something like:

jQuery.cache[ someInternalNodeID ][ theKey ] = theValue;

Everything goes into the same structure, including event handlers:

jQuery.cache[ someInternalNodeID ][ 'events' ][ 'click' ] = theHandler;

When an element is removed, jQuery can therefore throw away all the data (and the event handlers) with one simple operation:

delete jQuery.cache[ someInternalNodeID ];

Theoretically, you may therefore also remove jQuery without leaks occurring from any references. jQuery even supports multiple separate instances of the library, each holding it's own set of data or events.

You can see John Resig explaining this stuff in the "The DOM Is a Mess" presentation.

这篇关于我应该用jQuery的数据()监视什么样的内存泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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