jQuery在去除缓存数据的一个失误
2009-12-14 13:45
211 查看
如果独自放着jQuery做事,它绝对做得很好,但jQuery充许与其他库共存在,有些事就防不胜防了。看下面代码,
留意星号包围的区域,John Resig的想法是,如果把元素节点的对应的缓存体的键值对全部移除后,就把此缓存体从cache中去掉。举个通俗的例子,cache就是一家银行,页面上的元素节点(elem)就是一个个人,每个人可以通过data方法在银行里开个帐户(id),而存折就是elem的那个expando属性,这个帐户可以做存许多东西,每样东西都分开管理的,如果帐户里面的东西都取光就会自动注销这个帐户。嗯,本来就应该这样,没用的东西就应该收CG回收掉,节省内存。
下面是一个仿制品
//Object.prototype.aa = "司徒正美";
var a = '',
cache = { };//银行
cache.account = {};//其中一个空帐户
for(a in cache.account){
break;
}
//如果account里面还有其他东西,a就会置换成其他字符串,无法执行括号里的操作
alert(a)
if(!a){
alert("以下将执行注销操作!")
delete cache.account
}
//下面检测cache还有没有account这个属性
alert(cache.account)
运行代码
但如果jQuery与一些在Object原型进行了扩展的库共库呢?假设在它里面添加了个aa属性……
Object.prototype.aa = "司徒正美";
var a = '',
cache = { };//银行
cache.account = {};//其中一个空帐户
for(a in cache.account){
break;
}
//如果account里面还有其他东西,a就会置换成其他字符串,无法执行括号里的操作
alert(a)
if(!a){
alert("以下将执行注销操作!")
delete cache.account
}
//下面检测cache还有没有account这个属性
alert(cache.account)
运行代码
这时就无法清除缓存体了,遇到这篇文章提到的问题《Google Closure: 糟糕的JavaScript》,真是百密一疏!这也很好的教训了我们不要对Object.prototype这个最基本的东西做扩展,Prototype最近的版本也对此做了修正。那么如何排除干扰呢?
Object.prototype.aa = "司徒正美";
var a = '',
cache = { };//银行
cache.account = {};//其中一个空帐户
for(var key in cache.account ){
if (cache.account.hasOwnProperty(key)) {
a = key
}
}
//如果account里面还有其他东西,a就会置换成其他字符串,无法执行括号里的操作
alert(a)
if(!a){
alert("以下将执行注销操作!")
delete cache.account
}
//下面检测cache还有没有account这个属性
alert(cache.account)
运行代码
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回收掉,节省内存。
下面是一个仿制品
//Object.prototype.aa = "司徒正美";
var a = '',
cache = { };//银行
cache.account = {};//其中一个空帐户
for(a in cache.account){
break;
}
//如果account里面还有其他东西,a就会置换成其他字符串,无法执行括号里的操作
alert(a)
if(!a){
alert("以下将执行注销操作!")
delete cache.account
}
//下面检测cache还有没有account这个属性
alert(cache.account)
运行代码
但如果jQuery与一些在Object原型进行了扩展的库共库呢?假设在它里面添加了个aa属性……
Object.prototype.aa = "司徒正美";
var a = '',
cache = { };//银行
cache.account = {};//其中一个空帐户
for(a in cache.account){
break;
}
//如果account里面还有其他东西,a就会置换成其他字符串,无法执行括号里的操作
alert(a)
if(!a){
alert("以下将执行注销操作!")
delete cache.account
}
//下面检测cache还有没有account这个属性
alert(cache.account)
运行代码
这时就无法清除缓存体了,遇到这篇文章提到的问题《Google Closure: 糟糕的JavaScript》,真是百密一疏!这也很好的教训了我们不要对Object.prototype这个最基本的东西做扩展,Prototype最近的版本也对此做了修正。那么如何排除干扰呢?
Object.prototype.aa = "司徒正美";
var a = '',
cache = { };//银行
cache.account = {};//其中一个空帐户
for(var key in cache.account ){
if (cache.account.hasOwnProperty(key)) {
a = key
}
}
//如果account里面还有其他东西,a就会置换成其他字符串,无法执行括号里的操作
alert(a)
if(!a){
alert("以下将执行注销操作!")
delete cache.account
}
//下面检测cache还有没有account这个属性
alert(cache.account)
运行代码
相关文章推荐
- jquery、js去除浏览器(ff、ie)缓存问题
- 读jQuery之六(缓存数据)
- 一个JSON 实例: jQuery 解析JSON数据
- jquery autocomplete 自动完成插件常见错误-火狐不支持中文、查询数据存在缓存等
- Android--------多条目展示数据(一种类型只有一个图片,另一种有两个图片)将图片缓存到SD卡
- 基于jquery实现的一个选择中国大学的弹框 (数据、步骤、代码)
- jQuery缓存数据——仿Map
- 一个简单的JavaScript数据缓存系统实现代码
- jquery的两种重要的方法以及data方法缓存数据详解
- jQuery发送ajax请求,IE有缓存,数据不更新问题
- 一个JSON 实例: jQuery 解析JSON数据
- 利用jQuery的ajax得到的数据动态创建树,然后点击数节点,把对应的节点的节点信息赋值给某一个div,是上一篇的升级版
- 读jQuery之六 缓存数据功能介绍
- jQuery中数据缓存$.data的用法及源码完全解析
- jQuery缓存操作-cache数据
- jquery、js去除浏览器(ff、ie)缓存问题
- jQuery中的$.fn.data数据缓存
- jQuery源码分析-07数据缓存-Cache
- iOS开发:一个高仿美团的团购ipad客户端的设计和实现(功能:根据拼音进行检索并展示数据,离线缓存团购数据,浏览记录与收藏记录的批量删除等)
- 一个具有缓存数据功能的HttpWebRequest工具类