M
Matija Papec
I would like to be able to convert any reference (function, object, or
array) into unique string id and retrieve original reference afterwards
using same id.
The following code does this but only for functions, and for some
unknown reason does not work properly for other kind of references.
var func = function(){};
var ref_id = $GS(func);
// get original reference
var func_2 = $GO(ref_id);
function $GS (ref) { return jsref(ref, "gstr"); }
function $GO (s) { return jsref(s, "gobj"); }
function jsref (ref, what) {
// guess
if (!what) {
what = (typeof(ref) == "string") ? "gobj" : "gstr";
}
var c = jsref;
if (!c.str_ref) c.str_ref = {};
if (!c.ref_str) c.ref_str = {};
if (!c.n) c.n = 0;
if (what == "gstr") {
if (!c.ref_str[ref]) {
c.n++;
c.ref_str[ref] = c.n;
c.str_ref[c.n] = ref;
}
return "jsref:"+ c.ref_str[ref];
}
else if (what == "gobj") {
ref = ref.replace(/\D+/g, '');
return c.str_ref[ref];
}
return null;
}
array) into unique string id and retrieve original reference afterwards
using same id.
The following code does this but only for functions, and for some
unknown reason does not work properly for other kind of references.
var func = function(){};
var ref_id = $GS(func);
// get original reference
var func_2 = $GO(ref_id);
function $GS (ref) { return jsref(ref, "gstr"); }
function $GO (s) { return jsref(s, "gobj"); }
function jsref (ref, what) {
// guess
if (!what) {
what = (typeof(ref) == "string") ? "gobj" : "gstr";
}
var c = jsref;
if (!c.str_ref) c.str_ref = {};
if (!c.ref_str) c.ref_str = {};
if (!c.n) c.n = 0;
if (what == "gstr") {
if (!c.ref_str[ref]) {
c.n++;
c.ref_str[ref] = c.n;
c.str_ref[c.n] = ref;
}
return "jsref:"+ c.ref_str[ref];
}
else if (what == "gobj") {
ref = ref.replace(/\D+/g, '');
return c.str_ref[ref];
}
return null;
}