HighDots Forums  

Referencing an object with a string

Javascript JavaScript language (comp.lang.javascript)


Discuss Referencing an object with a string in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Andy Dietler
 
Posts: n/a

Default Referencing an object with a string - 08-02-2006 , 04:36 PM






If I an objects called: MetaData1, MetaData2, MetaData3, etc. and I
want to use a loop to get the value of say each object's length how can
I do that?

for example:

for (i=1;i<=5;i++) {
alert(MetaData + i.length);
}

Obviously that isn't valid.


Reply With Quote
  #2  
Old   
Michael Winter
 
Posts: n/a

Default Re: Referencing an object with a string - 08-02-2006 , 05:54 PM






On 02/08/2006 21:36, Andy Dietler wrote:

Quote:
If I an objects called: MetaData1, MetaData2, MetaData3, etc. and I
want to use a loop to get the value of say each object's length how can
I do that?
Perhaps the simplest approach to have an array of MetaData. If that's
not possible for some reason, then the identifier may be constructed at
run-time and the object can be referenced as a property of some object.

For example, consider the following variables to be global:

var object1 = {},
object2 = {},
object3 = {};

As such, they are properties of the global object. This can be
referenced using the this operator (outside the scope of methods -
functions called as properties of an object - and constructor
functions), or the window object in browsers:

for (var i = 1; i <= 3; ++i) {

// Do something with: this["object" + i]

}

Mike


Reply With Quote
  #3  
Old   
Vic Sowers
 
Posts: n/a

Default Re: Referencing an object with a string - 08-02-2006 , 07:28 PM




"Andy Dietler" <andydietler (AT) gmail (DOT) com> wrote

Quote:
If I an objects called: MetaData1, MetaData2, MetaData3, etc. and I
want to use a loop to get the value of say each object's length how can
I do that?

for example:

for (i=1;i<=5;i++) {
alert(MetaData + i.length);
}

Obviously that isn't valid.

Use the evil 'eval' function:

alert(eval("MetaData"+i+".length"));




Reply With Quote
  #4  
Old   
Richard Cornford
 
Posts: n/a

Default Re: Referencing an object with a string - 08-02-2006 , 07:50 PM



Vic Sowers wrote:
<snip>
Quote:
Use the evil 'eval' function:

alert(eval("MetaData"+i+".length"));
One of the things that makes the - eval - function evil is that people
recommend its use where its use is not necessary and it becomes a means
by which people can do badly what could otherwise be done using normal
language constructs. That is, its use often stands as a barrier to
people learning javascript.

It is never necessary to use - eval - to reference a globally declared
variable as it is always possible to easily acquire a reference to the
global object, and then use a bracket notation property accessor to
reference its property.

<URL: http://jibbering.com/faq/#FAQ4_39 >

And good code design can eliminate any need to use - eval - to reference
object properties/variables at all.

<URL: http://jibbering.com/faq/#FAQ4_40 >

Richard.




Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.