hello
Function.prototype.memorize = function(timer){
var obj = {},
that = this,
key = "$";
return function(flag){
var args = [].slice.call(arguments,1) || [];
if(flag){
return obj[key] = that.apply(this,args);
}
key = [].join.call(arguments,"$");
if(timer){
setTimeout(function(){delete obj[key];},timer);
}
return obj[key] ? obj[key] : obj[key] = that.apply(this,args);
}
}
var div = document.getElementById("test");
var length = function(){
return div.getElementsByTagName("p").length;
}.memorize();
alert(length());
div.appendChild(document.createElement("p"));
alert(length(true));
div.appendChild(document.createElement("p"));
alert(length());
div.appendChild(document.createElement("p"));
alert(length(true));
div.appendChild(document.createElement("p"));
alert(length());