By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,190 Members | 1,056 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,190 IT Pros & Developers. It's quick & easy.

use string to get access to an object

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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.