use string to get access to an object

A

andy_w_irvine

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.
 
R

RobG

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
// ...
}

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>
 

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,075
Messages
2,570,564
Members
47,200
Latest member
Vanessa98N

Latest Threads

Top