Relator object proposal
Relator object proposal
Hi everybody.
Yesterday I was developing a really weird script, and I was missing a
better way to cache informations, generic object related.
The jQuery.cache object is something "id dedicated".
Accordingly, it is not able to cache entire results, nodes, binded
function, because of its simple hashtable nature, based on string
keys.
For this reason, I have created an object, called Relator, that is
capable to "cache once" every kind of variable, included undefined,
null, booleans, functions, constructors, prototypes, objects, events,
DOM nodes, etc etc.
I wonder if this Object concept, could be useful inside jQuery core,
since possibility are hundreds (it is not limited to ids) and the code
is nearly "ridiculous" for its simplicity and size.
var Relator = function(Array, Object){
// (c) Andrea Giammarchi - Mit Style License
if(!Array.indexOf)
Array.indexOf = function(value){
for(var i = 0, length = Array.length; i < length; i++)
if(Array[i] === value)
return i;
return -1
};
return {
get:function(value){
return Object[Array.indexOf(value)]
},
set:function(value){
var i = Array.indexOf(value);
return ~i ? Object[i] : Object[Array.push(value) - 1] =
{}
},
del:function(value){
var i = Array.indexOf(value);
if(~i){
Array.splice(i, 1);
Object.splice(i, 1);
};
return this
}
}
}([], []);
A better explanation about this object, if interested, could be found
in my blog ( webreflection )
Kind Regards,
Andrea