473,513 Members | 2,409 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Creating References To Strings

So I wanted an object to have a reference to a string in side of an
array some where else and so here is how I made that possible:

<div id="something"> </div>

<script>

var obj = document.getElementById('something');

var vars = {
name : 'bob',
color : 'blue',
}
// the problem

obj.varRef = vars.name; // this copies the value not a ref

// so

obj.varRef = 'sam'; // this doesnt update vars.name

// my solution

/*
stringRef requires the following
- the reference your are making is inside of an object
- this is so we can use watch
- that you know the absolute call to the var you are referencing
- this is so we can use it in an eval
- and the name of the ref object we are creating
- but of course you would know this.

i realize a lot, but not all, of the many limitatons this has. got
any better ideas?
*/

document.stringRef = Class.create();
document.stringRef.prototype = {
initialize : function (parent, myname, ref) {
this.ref = ref;
var that = this;
parent.watch (myname, function (id,oldval,newval) {
eval( that.ref+' = "'+newval+'"' );
return that;
});

},
toString : function () { return eval(this.ref); },
};

// how to use it

obj.varRef = new document.stringRef( obj, 'varRef', 'vars.name' );

check();
vars.name = 'bob2';
check();
obj.varRef = 'paul';
check();
function check () {
document.writeln(
'vars.name : '+vars.name+'<br>'+
'obj.varRef : '+obj.varRef+'<br>'
);
}

</script>
any suggestions? thoughts?

I was bumbed i couldnt do this so i made something that worked but
maybe there is no need for this?

thanks

May 29 '06 #1
4 1226
deadlyicon wrote:
So I wanted an object to have a reference to a string in side of an
array some where else and so here is how I made that possible:
[...]
any suggestions? thoughts?


It is using Prototype junk and evil eval()[tm] needlessly within not Valid
markup; I will not even bother to comment on the details as we have
discussed them /ad nauseam/ before. Please get informed to do much better
before you propose anything here again. TIA.

<URL:http://jibbering.com/faq/>
PointedEars
--
Homer: I have changed the world. Now I know how it feels to be God!
Marge: Do you want turkey sausage or ham?
Homer: Thou shalt send me *two*, one of each kind.
(Santa's Little Helper [dog] and Snowball [cat] run away :))
May 29 '06 #2
so what your saying is I should learn how to not be stupid before I
show my face around you again?

I just want to be clear
Jared

Thomas 'PointedEars' Lahn wrote:
deadlyicon wrote:
So I wanted an object to have a reference to a string in side of an
array some where else and so here is how I made that possible:
[...]
any suggestions? thoughts?


It is using Prototype junk and evil eval()[tm] needlessly within not Valid
markup; I will not even bother to comment on the details as we have
discussed them /ad nauseam/ before. Please get informed to do much better
before you propose anything here again. TIA.

<URL:http://jibbering.com/faq/>
PointedEars
--
Homer: I have changed the world. Now I know how it feels to be God!
Marge: Do you want turkey sausage or ham?
Homer: Thou shalt send me *two*, one of each kind.
(Santa's Little Helper [dog] and Snowball [cat] run away :))


May 31 '06 #3

deadlyicon wrote:

[snip]
document.stringRef = Class.create();
document.stringRef.prototype = {
initialize : function (parent, myname, ref) {
this.ref = ref;
var that = this;
parent.watch (myname, function (id,oldval,newval) {
eval( that.ref+' = "'+newval+'"' );
return that;
});

},
toString : function () { return eval(this.ref); },
};


I am no expert, but some observations are:-

1. Your reference to "Class.create" indicates that you are using
"prototype.js". That library tends to arouse a lot of debate and
argument in this newsgroup. A common point seems to be that if you are
going to use it (or indeed any library), try to become familiar with
the underlying code and any limitations it may have. I don't mean to
imply that you don't already have this understanding.

2. Your reference to "eval" again also tends to arouse a lot of debate
and argument, partly I think because it carries a lot of overheads. It
is not invalid to use eval, but the concensus seems to be that eval
should only be used if there is no alternative.

For instance, in your code, you could try replacing eval as follows
(simplified):-

function propertyConnector(obj2,obj2propertyname,obj1,obj1p ropertyname)
{
var SELF=this;

obj2.watch (obj2propertyname, function
(obj2propertyname,oldval,newval){
obj1[obj1propertyname]=newval;
return SELF;
});

this.toString=function () { return
obj1[obj1propertyname];}
};

var OBJ1 = {
name : 'bob',
color : 'blue'
};

var OBJ2={};
OBJ2.myProp=new propertyConnector(OBJ2,"myProp",OBJ1,"name");

3. I don't think "watch" is available for JScript 5.6 in
InternetExplorer so your solution may not be cross-browser. There is
no easy way to do this in JScript 5.6 I don't think.

Something (roughly) like:-

function propertyConnector(obj1,obj1propertyname)
{
var SELF=this;

this.set=function(newval)
{
obj1[obj1propertyname]=newval;
};

this.toString=this.get=function () { return
obj1[obj1propertyname];}
};

var OBJ1 = {
name : 'bob',
color : 'blue'
};

var OBJ2={};
OBJ2.myProp=new propertyConnector(OBJ1,"name");

OBJ2.myProp.set("Paul");

Not really that helpful though.

4. Finally, if you could explain why you need to synchronise
properties of two objects, there may be some completely different
solution that fits your purposes.

Regards

Julian Turner

May 31 '06 #4
deadlyicon said the following on 5/31/2006 12:26 AM:
so what your saying is I should learn how to not be stupid before I
show my face around you again?


Welcome to comp.lang.javascript and the moron known as Thomas who thinks
he is the Almighty being when in reality he wouldn't make a pimple on
the Almighty's posterior.

P.S. Tell him before he rants about behavior in Usenet that he should
use a proper signature.

P.S.S.
Answer:It destroys the order of the conversation
Question: Why?
Answer: Top-Posting.
Question: Whats the most annoying thing on Usenet?

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
May 31 '06 #5

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

Similar topics

23
40592
by: Fuzzyman | last post by:
Pythons internal 'pointers' system is certainly causing me a few headaches..... When I want to copy the contents of a variable I find it impossible to know whether I've copied the contents *or*...
3
2111
by: bk3 | last post by:
I'm having a bit of a problem creating foreign keys on a MySQL database. The tables create and everything seems to be fine. But I just wanted to test out adding a row to one of the child tables...
7
21444
by: Justin | last post by:
I am extremely new at SQL Server2000 and t-sql and I'm looking to create a simple trigger. For explanation sake, let's say I have 3 columns in one table ... Col_1, Col_2 and Col_3. The data type...
7
1415
by: Daniel | last post by:
how to make two references to one string that stay refered to the same string reguardless of the changing value in the string?
5
3539
by: Barbara Lindsey | last post by:
Thank you for your help on the trigger question. The RULE worked for most of the cases I had for this, but I have one that is giving me trouble. Here are my table definitions: CREATE SEQUENCE...
12
3139
by: Mats Lycken | last post by:
Hi, I'm creating a CMS that I would like to be plug-in based with different plugins handling different kinds of content. What I really want is to be able to load/unload plugins on the fly without...
15
2791
by: David Thielen | last post by:
Hi; My ASP.NET app (C# calling J# under .net 2.0) creates a png file in a subdirectory to display as part of the created page. However, the bitmap will not display due to a security violation. ...
2
3953
by: astolpho | last post by:
I am using a slightly outdated reference book on J2EE programming. It gives 2 methods of creating a database used in its casestudies. The first is an ANT script that gives the following output: ...
32
2502
by: Joe | last post by:
I am just starting to use Object Oriented PHP coding, and I am seeing quite often the following (this example taken from a wiki): $wakka =& new Wakka($wakkaConfig); What exactly is the =&, and...
0
7175
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...
1
7120
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...
0
5697
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,...
0
4754
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...
0
3247
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...
0
3235
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1609
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 ...
1
809
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
466
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...

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.