Creating References To Strings

D

deadlyicon

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
 
T

Thomas 'PointedEars' Lahn

deadlyicon said:
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
 
D

deadlyicon

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
deadlyicon said:
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 :))
 
J

Julian Turner

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,obj1propertyname)
{
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
 
R

Randy Webb

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top