473,466 Members | 1,508 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Referencing an object with a string

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.

Aug 2 '06 #1
3 1229
On 02/08/2006 21:36, Andy Dietler wrote:
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
Aug 2 '06 #2

"Andy Dietler" <an*********@gmail.comwrote in message
news:11**********************@75g2000cwc.googlegro ups.com...
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"));
Aug 2 '06 #3
Vic Sowers wrote:
<snip>
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.
Aug 2 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Dan Bass | last post by:
Say I've got an ArrayList, which contains a list of integers. Is it possible to get a reference to one of the integers contained within the collection so that changes to it are reflected in the...
2
by: HankD | last post by:
Hi, I am having a problem with instantiating two custom objects so they DO NOT point to the same memory location. What is happening is that changes I am making to my object1 are changing object2. I...
1
by: DR | last post by:
when i reference a com object by regsvr32 it then find it in the COM tab, it works ok. but when i reference a com object by referencing the .dll file i get this error in my C# application: ...
1
by: DR | last post by:
when i reference a com object by regsvr32 it then find it in the COM tab, it works ok. but when i reference a com object by referencing the .dll file i get this error in my C# application: ...
2
by: Andrus | last post by:
I need compile in-memory assembly which references to other in-memory assembly. Compiling second assembly fails with error Line: 0 - Metadata file 'eed7li9m, Version=0.0.0.0, Culture=neutral,...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.