469,352 Members | 1,797 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,352 developers. It's quick & easy.

use string to get access to an object

Hi,

Apologies if this has been asked elsewhere, I did search but expect I
didn't hit on the required terminology.

I want to be able to use a string variable called objectName
(original!) to get a reference to an instance of my object
courseObject - created using the following code.

function courseObject(courseName,sectionX,sectionY,layer){
this.courseName=courseName;
this.sectionX=sectionX;
this.sectionY=sectionY;
this.layer=layer;

this.updateLocation=updateLocation;
this.updateLayer=updateLayer;
}

+ simple methods

objectName gets its value from the following section(comes from a drag
and drop API that provides the name of the element that was last
dragged)
objectName= dd.obj.name

There is an object created for each element of the drag & drop
interface. I want to use the string objectName to get a reference to
the object with the name the string contains. To be used in this
fashion.

objectName.updateLocation(section,day)

But as objectName is a string rather than a reference to the object it
will not work. Sure there is a simple solution but I am missing it. Any
help much appreciated.

Feb 1 '06 #1
1 1200
an***********@hotmail.com wrote:
Hi,

Apologies if this has been asked elsewhere, I did search but expect I
didn't hit on the required terminology.

I want to be able to use a string variable called objectName
(original!) to get a reference to an instance of my object
courseObject - created using the following code.

function courseObject(courseName,sectionX,sectionY,layer){
this.courseName=courseName;
this.sectionX=sectionX;
this.sectionY=sectionY;
this.layer=layer;

this.updateLocation=updateLocation;
this.updateLayer=updateLayer;
}

+ simple methods

objectName gets its value from the following section(comes from a drag
and drop API that provides the name of the element that was last
dragged)
objectName= dd.obj.name
Variables created outside the scope of some other object or without the
'var' keyword are global. You should still declare them with the var
keyword. If they are intended to be global, declare them right at the
start of the script:

<script type="text/javascript">

var objectName;

// more stuff

function someFn()
{
objectName= dd.obj.name
// ...
}

</script>
There is an object created for each element of the drag & drop
interface. I want to use the string objectName to get a reference to
the object with the name the string contains. To be used in this
fashion.

objectName.updateLocation(section,day)
window[objectName].updateLocation(section,day)
Where window represents the global object. A more robust solution is to
create your own 'global' variable that refers to the global object (just
in case it isn't 'window') right at the very start of the script:

var global = this;
Now you can do:

global[objectName].updateLocation(section,day)
anywhere, though you may want to test that global[objectName] exists
before trying to use it, say with:

if (global[objectName]) {
global[objectName].updateLocation(section,day)
}

or

global[objectName] && global[objectName].updateLocation(section,day)


But as objectName is a string rather than a reference to the object it
will not work. Sure there is a simple solution but I am missing it. Any
help much appreciated.


Read the FAQ on references using square brackets.

<URL:http://www.jibbering.com/faq/#FAQ4_39>
--
Rob
Feb 1 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Ronald Fischer | last post: by
8 posts views Thread by Grant Wagner | last post: by
4 posts views Thread by mrgr8avill | last post: by
3 posts views Thread by Justnew | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.