日期:2014-05-16 浏览次数:20530 次
测试通过
var adder = function(n){ var t = 10; var org = n+"-"+Math.random(); return{ getinfo:function(){ document.writeln("\n\t"+org); return n+t; } }; }; var CachedSearchBox = (function(){ var cache = {},count = [],totalCount = 20; return { attachSearchBox:function(dsid){ if(dsid in cache){ print("old:::"+cache[dsid].getinfo()); return cache[dsid]; } var fsb = new adder(dsid); print("new::::"+fsb.getinfo()); cache[dsid] = fsb; if(count.length>=totalCount){ var out = count.shift(); print("del::::::::::::"+out.getinfo()) delete cache[out]; } count.push(fsb); return fsb; }, clearSearchBox:function(dsid){ if(dsid in cache){ //cache[dsid].clearSelection(); delete cache[dsid]; count.shift(); } }, getcount:function(){//获取对象的长度 print("array length:"+count.length); return count.length; }, setTotalCount:function(c){//设置缓存大小 totalCount = c; } }; })(); CachedSearchBox.getcount(); print("<br>"); (function(){ for(var i=0;i<100;i++){ CachedSearchBox.attachSearchBox(Math.ceil(Math.random()*120)) print("~~~~~"); CachedSearchBox.getcount(); print("<br>"); if(i==30) CachedSearchBox.setTotalCount(40); } })(); function print(a1){ document.writeln(a1+"\n\t"); }
?