jQuery在去除缓存数据的一个失误

来源:Ruby's Louvre 时间:2009-12-14 20:40:00 

如果独自放着jQuery做事,它绝对做得很好,但jQuery充许与其他库共存在,有些事就防不胜防了。看下面代码


data :function (elem, name,data){ 
//略 
}, 
removeData: function (elem, name) { 
elem = elem == window ? windowData : elem; 
var id = elem[expando]; 
// If we want to remove a specific section of the element's data 
if (name) { 
if (jQuery.cache[id]) { 
//★★★★★★★ 
// Remove the section of cache data 
delete jQuery.cache[id][name]; 
// If we've removed all the data, remove the element's cache 
name = ""; 
for (name in jQuery.cache[id]) 
break; 
if (!name) jQuery.removeData(elem); 
//★★★★★★★ 

// Otherwise, we want to remove all of the element's data 
} else { 
// Clean up the element expando 
try { 
delete elem[expando]; 
} catch(e) { 
// IE has trouble directly removing the expando 
// but it's ok with using removeAttribute 
if (elem.removeAttribute) elem.removeAttribute(expando); 

// Completely remove the data cache 
delete jQuery.cache[id]; 

},

留意星号包围的区域,John Resig的想法是,如果把元素节点的对应的缓存体的键值对全部移除后,就把此缓存体从cache中去掉。举个通俗的例子,cache就是一家银行,页面上的元素节点(elem)就是一个个人,每个人可以通过data方法在银行里开个帐户(id),而存折就是elem的那个expando属性,这个帐户可以做存许多东西,每样东西都分开管理的,如果帐户里面的东西都取光就会自动注销这个帐户。嗯,本来就应该这样,没用的东西就应该收CG回收掉,节省内存。

下面是一个仿制品

但如果jQuery与一些在Object原型进行了扩展的库共库呢?假设在它里面添加了个aa属性……

这时就无法清除缓存体了,遇到这篇文章提到的问题《Google Closure: 糟糕的JavaScript》,真是百密一疏!这也很好的教训了我们不要对Object.prototype这个最基本的东西做扩展,Prototype最近的版本也对此做了修正。那么如何排除干扰呢?


标签:jquery,缓存,数据
0
投稿

猜你喜欢

  • 简介Django中内置的一些中间件

    2023-03-16 19:20:27
  • vue中的ref和$refs的使用

    2024-05-13 09:08:44
  • MySQL 序列 AUTO_INCREMENT详解及实例代码

    2024-01-20 17:04:37
  • python音频处理用到的操作的示例代码

    2021-05-11 22:05:19
  • Python DPED机器学习之实现照片美化

    2023-11-25 01:50:24
  • JQuery调用绑定click事件的3种写法

    2024-04-09 19:48:14
  • nginx+uwsgi+django环境搭建的方法步骤

    2022-01-12 16:34:59
  • asp正则表达式详细说明

    2008-10-23 16:23:00
  • 下一站:HandlerSocket!

    2011-04-11 09:02:00
  • 在Python 字典中一键对应多个值的实例

    2023-07-25 23:45:02
  • vue2.0+webpack环境的构造过程

    2024-05-08 10:42:00
  • C#调用Python模块的方法

    2021-04-13 15:29:10
  • python基础教程之元组操作使用详解

    2023-11-29 01:18:52
  • Python使用xlrd模块操作Excel数据导入的方法

    2022-05-03 19:28:24
  • 用javascript实现select的美化

    2007-05-11 16:50:00
  • 详解如何使用Pytorch进行多卡训练

    2023-08-02 06:43:29
  • mysql Innodb表空间卸载、迁移、装载的使用方法

    2024-01-25 16:41:56
  • Nodejs 和Session 原理及实战技巧小结

    2024-05-13 10:06:08
  • 深入解析Python的Tornado框架中内置的模板引擎

    2022-08-03 12:43:52
  • 在PyCharm的 Terminal(终端)切换Python版本的方法

    2021-10-31 08:37:07
  • asp之家 网络编程 m.aspxhome.com