473,796 Members | 2,712 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

unique reference id


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;
}
Oct 14 '08 #1
17 1729
Matija Papec schreef:
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;
}
Hi,

Just curious: Why don't you simply store the reference itself?

Regards,
Erwin Moller

--
Oct 14 '08 #2
Erwin Moller wrote:
> }
return null;
}

Hi,

Just curious: Why don't you simply store the reference itself?
The reason is ajax.
When making an ajax call, client side is deciding what reference to pass
to server, so client can later operate on the same reference.

Unfortunately references does not survive ajax calls, so there is a need
for unique string representation of reference.
Oct 14 '08 #3
Matija Papec schreef:
Erwin Moller wrote:
>> }
return null;
}
Hi,

Just curious: Why don't you simply store the reference itself?

The reason is ajax.
When making an ajax call, client side is deciding what reference to pass
to server, so client can later operate on the same reference.

Unfortunately references does not survive ajax calls, so there is a need
for unique string representation of reference.
Why don't you make a simple counter (incremental number) and put the
reference as a number in an array or object?
Pass the number via Ajax around, and when the server answer, use the
number to look up the reference.

Regards,
Erwin Moller

--
Oct 14 '08 #4
Erwin Moller schreef:
Matija Papec schreef:
>Erwin Moller wrote:
>>> }
return null;
}
Hi,

Just curious: Why don't you simply store the reference itself?

The reason is ajax.
When making an ajax call, client side is deciding what reference to pass
to server, so client can later operate on the same reference.

Unfortunatel y references does not survive ajax calls, so there is a need
for unique string representation of reference.

Why don't you make a simple counter (incremental number) and put the
reference as a number in an array or object?
That was poorly phrased.
I mean something like this:

var count=0;
var myRefs = new Array();
function getANumberForTh isRef(aRef){
count++;
myRefs[count] = aRef;
}

// do your Ajaxstuff here

function getARefForThisN umber(aNum){
return myRefs[aNum];
}

Regards,
Erwin Moller
Pass the number via Ajax around, and when the server answer, use the
number to look up the reference.

Regards,
Erwin Moller

--
Oct 14 '08 #5
Erwin Moller schreef:
Erwin Moller schreef:
>Matija Papec schreef:
>>Erwin Moller wrote:
}
return null;
}
Hi,

Just curious: Why don't you simply store the reference itself?

The reason is ajax.
When making an ajax call, client side is deciding what reference to pass
to server, so client can later operate on the same reference.

Unfortunate ly references does not survive ajax calls, so there is a need
for unique string representation of reference.

Why don't you make a simple counter (incremental number) and put the
reference as a number in an array or object?

That was poorly phrased.
I mean something like this:

var count=0;
var myRefs = new Array();
function getANumberForTh isRef(aRef){
count++;
myRefs[count] = aRef;
-- return count;
}
/me gets a coffee....

Regards,
Erwin Moller

>
// do your Ajaxstuff here

function getARefForThisN umber(aNum){
return myRefs[aNum];
}

Regards,
Erwin Moller
>Pass the number via Ajax around, and when the server answer, use the
number to look up the reference.

Regards,
Erwin Moller


--
Oct 14 '08 #6
Erwin Moller wrote:
>>Why don't you make a simple counter (incremental number) and put the
reference as a number in an array or object?

That was poorly phrased.
I mean something like this:

var count=0;
var myRefs = new Array();
function getANumberForTh isRef(aRef){
count++;
myRefs[count] = aRef;
Yes, I'm already doing this[1], but unfortunately getting same id for
two different objects,
http://www.mcs-informatika.hr/~mcs_matija/js/
[1] I'm also taking care so that the same reference always corresponds
to the same "count" number

>}

/me gets a coffee....
:)

Oct 14 '08 #7
Matija Papec schreef:
Erwin Moller wrote:
>>>Why don't you make a simple counter (incremental number) and put the
reference as a number in an array or object?
That was poorly phrased.
I mean something like this:

var count=0;
var myRefs = new Array();
function getANumberForTh isRef(aRef){
count++;
myRefs[count] = aRef;

Yes, I'm already doing this[1], but unfortunately getting same id for
two different objects,
http://www.mcs-informatika.hr/~mcs_matija/js/
[1] I'm also taking care so that the same reference always corresponds
to the same "count" number
Hi,

I checked your code and added some comments after //.
<script>

alert($GS( {foo:1} ));
alert($GS( {bar:2} ));
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";
}

// OK, so here you make c pointing to some reference.
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]) {
// Here you ADD a property to c, so that is a property for each object.
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;
}
</script>
Conclusion: You are NOT doing what I suggested.
You are adding the n property to your object: poluting your objects with
that, and possibly overwriting existing ones (I cannot tell).

Solution: Review my earlier posted code and use that approach. Use 1
counter for all and don't touch the objects the references point to.

Disclaimer:
Possibly I am missing something since I never use $ in Javascript in the
way you do. I only use $ in regular expressions in JavaScript.
I looked it up in my Definitive Guide 4th edition, but it is not listed
in there in the way you use it.
So maybe I miss something important. ;-)

I have no time right now to investigate the $ futher, but hope my
comments help nonetheless

Regards,
Erwin Moller
>
>>}
/me gets a coffee....

:)

--
Oct 15 '08 #8
Matija Papec wrote:
Erwin Moller wrote:
>>>Why don't you make a simple counter (incremental number) and put the
reference as a number in an array or object?
That was poorly phrased.
I mean something like this:

var count=0;
var myRefs = new Array();
function getANumberForTh isRef(aRef){
count++;
myRefs[count] = aRef;

Yes, I'm already doing this[1], but unfortunately getting same id for
two different objects,
That can only be because either you do not test whether you already stored a
reference to the same object, or because there are different objects.
http://www.mcs-informatika.hr/~mcs_matija/js/
q.e.d.

`{x: 1}' and `{x: 1}' (and `[x]' and `[x]') result in references to
*different* objects ({x: 1} != {x: 1}). Each Object (and Array) object
initializer creates a new Object(/Array) object and results in a reference
to it. (In contrast to RegExp object initializers; however, /x/ != /x/ by
definition.)

So what you really want to test is whether two objects have the same
(enumerable) properties and whether those properties have the same value.
One general way to do that:

var equal = true;
for (var p in o1)
{
if (!o2.hasOwnProp erty(p) || o2[p] !== o1[p])
{
equal = false;
break;
}
}

for (var p in o2)
{
if (!o1.hasOwnProp erty(p) || o1[p] !== o2[p])
{
equal = false;
break;
}
}

Object.prototyp e.hasOwnPropert y() requires an implementation of ECMAScript
Ed. 3. You can achieve greater compatibility but less precision with an
alternative to hasOwnProperty( ) and, optionally, also with loose comparison
(`!=').

Also note that this needs to be improved for deep compare, and for inherited
enumerable properties.
PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
Oct 15 '08 #9
Thomas 'PointedEars' Lahn wrote:
>>> myRefs[count] = aRef;
Yes, I'm already doing this[1], but unfortunately getting same id for
two different objects,

That can only be because either you do not test whether you already stored a
reference to the same object, or because there are different objects.
>http://www.mcs-informatika.hr/~mcs_matija/js/

q.e.d.

`{x: 1}' and `{x: 1}' (and `[x]' and `[x]') result in references to
*different* objects ({x: 1} != {x: 1}).
Yes, of course they refer to different objects and I'm expecting such
behavior.
In the mean time I found what was the problem when looking for object id
in lookup table.
var h1 = {foo: 33};
var h2 = {foo: 33};
// false as expected
alert(h1==h2);
var lookup = {};
lookup[h1] = true;
// true, but I'm expecting false or undefined
alert(lookup[h2]);

It seems that references cannot be used as *object keys* so lookup fails
when searching for reference.

>Each Object (and Array) object
initializer creates a new Object(/Array) object and results in a reference
to it. (In contrast to RegExp object initializers; however, /x/ != /x/ by
definition.)

So what you really want to test is whether two objects have the same
(enumerable) properties and whether those properties have the same value.
One general way to do that:
Yes, that would be solution for comparing object properties.
tnx
Oct 17 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
8133
by: Westcoast Sheri | last post by:
Which will be a faster lookup of an item by, "color" in the following mySQL tables: "unique key," "primary key," or just plain "key"?? CREATE TABLE myTable ( number int(11) NOT NULL default '0', description varchar(50) NOT NULL default '', color varchar(30) NOT NULL default '', price decimal(3,2) NOT NULL default '0.00', UNIQUE KEY (color) );
26
45452
by: Agoston Bejo | last post by:
I want to enforce such a constraint on a column that would ensure that the values be all unique, but this wouldn't apply to NULL values. (I.e. there may be more than one NULL value in the column.) How can I achieve this? I suppose I would get the most-hated "table/view is changing, trigger/function may not see it" error if I tried to write a trigger that checks the uniqueness of non-null values upon insert/update.
4
1378
by: João Santa Bárbara | last post by:
Hi all i have a problem with my sql dataadapter, i think. i have this table below ( Script ) and this table has one particularity, it has 2 unique constraints. when i´m filling my datatable using my dataadapter, the datatable only have one ??? is there any workarround for it, besides do it by code ???
12
2710
by: Russell E. Owen | last post by:
I have several situations in my code where I want a unique identifier for a method of some object (I think this is called a bound method). I want this id to be both unique to that method and also stable (so I can regenerate it later if necessary). I thought the id function was the obvious choice, but it doesn't seem to work. The id of two different methods of the same object seems to be the same, and it may not be stable either. For...
6
5550
by: Poul Møller Hansen | last post by:
I have made a stored procedure, containing this part for generating a unique reference number. SET i = 0; REPEAT SET i = i + 1; SELECT RAND() INTO reference FROM SYSIBM.SYSDUMMY1; SET p_reference = ref_prefix || SUBSTR(CAST(reference AS CHAR(12)),3);
5
16737
by: aj | last post by:
DB2 WSE 8.1 FP5 Red Hat AS 2.1 What is the difference between adding a unique constraint like: ALTER TABLE <SCHEMA>.<TABLE> ADD CONSTRAINT CC1131378283225 UNIQUE ( <COL1>) ; and adding a unique index like:
4
4029
by: Sally Sally | last post by:
Hi all, I am wandering about the pros and cons of creating a separate serial field for a primary key when I already have a single unique field. This existing unique field will have to be a character of fixed length (VARCHAR(12)) because although it's a numeric value there will be leading zeroes. There are a couple more tables with similar unique fields and one of them would need to reference the others. Does anybody see any good reason for...
10
14704
by: Laurence | last post by:
Hi there, How to differentiate between unique constraint and unique index? These are very similar but I cannot differentiate them? Could someone give me a hand? Thanks in advance
5
9735
by: Davinder | last post by:
Hi, I'm trying to find out how unique a GUID is? Basically I have two web applications and I need to pass a couple of session variables between them using a table in a database. I have set this up and used a GUID as a key for the table and it all seems to work fine but I'm a little concerned about the uniqueness of the GUID. Does anyone know how the GUID is created?
0
9524
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10168
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10003
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9047
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6785
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5568
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4114
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.