Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / HTML, CSS, Scripts / JavaScript / May 2006



Tip: Looking for answers? Try searching our database.

Creating References To Strings

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
deadlyicon - 29 May 2006 04:53 GMT
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
Thomas 'PointedEars' Lahn - 29 May 2006 13:32 GMT
> 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
Signature

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 :))

deadlyicon - 31 May 2006 05:26 GMT
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

> > 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:
[quoted text clipped - 14 lines]
> Homer: Thou shalt send me *two*, one of each kind.
> (Santa's Little Helper [dog] and Snowball [cat] run away :))
Randy Webb - 31 May 2006 16:08 GMT
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?

Signature

Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

Julian Turner - 31 May 2006 09:18 GMT
[snip]
>         document.stringRef = Class.create();
>         document.stringRef.prototype = {
[quoted text clipped - 9 lines]
>             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
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.