473,386 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

dynamic variables

Hi,

I need to create a variable out of nothing. From a database I extract an
item with a certain id. With this id I want to create a new variable.
For example:

id = 36;

"item"+id = new Array();

Now I get the message "Illegal left hand assignment".

I tried:

eval("item"+id) = new Array();

Is it possible to create a variable out of nothing?

---

J.P.
Jul 23 '05 #1
4 1358
On 06/05/2005 21:05, J.P. wrote:
I need to create a variable out of nothing. From a database I extract an
item with a certain id. With this id I want to create a new variable.
For example:

id = 36;

"item"+id = new Array();


[snip]

There are two options that immediately spring to mind. They're basically
the same, but I prefer the latter.

1) Create these variables on the global object:

/* In global scope: */
var global = this;

/* ... */

/* In any scope: */
global['item' + id] = []; /* Create new array */

which can also be written:

window['item' + id] = [];

2) Create an object that will do the same thing as the global
object, above:

var items = {}; /* Create an object, items
* {} is an object literal,
* equivalent to new Object()
*/

/* ... */

items[id] = []; // Create new array
The notation, identifier[...], has nothing to do with arrays in this
situation. Square bracket notation, as it is known, is effectively the
same as regular dot notation used to access object members, except that
it allows the use of expressions to compose the property name. See the
relevant FAQ notes article[1] for more information.

Hope that helps,
Mike
[1] <URL:http://www.jibbering.com/faq/faq_notes/square_brackets.html>

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #2
Lee
J.P. said:

Hi,

I need to create a variable out of nothing. From a database I extract an
item with a certain id. With this id I want to create a new variable.
For example:

id = 36;

"item"+id = new Array();

Now I get the message "Illegal left hand assignment".

I tried:

eval("item"+id) = new Array();

Is it possible to create a variable out of nothing?


var dynamic = new Object();
dynamic["item"+id] = new Array();
dynamic["item"+id][0] = "somevalue";

Jul 23 '05 #3
Michael Winter schreef:
<remove>
2) Create an object that will do the same thing as the global
object, above:

var items = {}; /* Create an object, items
* {} is an object literal,
* equivalent to new Object()
*/

/* ... */

items[id] = []; // Create new array
The notation, identifier[...], has nothing to do with arrays in this
situation. Square bracket notation, as it is known, is effectively the
same as regular dot notation used to access object members, except that
it allows the use of expressions to compose the property name. See the
relevant FAQ notes article[1] for more information.

Hope that helps,
Mike
[1] <URL:http://www.jibbering.com/faq/faq_notes/square_brackets.html>


Yes, that really helps. Thanks a lot!! My code is readable again!

--

J.P.
Jul 23 '05 #4
J.P. wrote:
Michael Winter schreef:
<remove>
2) Create an object that will do the same thing as the global
object, above:

var items = {}; /* Create an object, items
* {} is an object literal,
* equivalent to new Object()
*/

/* ... */

items[id] = []; // Create new array
The notation, identifier[...], has nothing to do with arrays in this
situation. Square bracket notation, as it is known, is effectively the
same as regular dot notation used to access object members, except
that it allows the use of expressions to compose the property name.
See the relevant FAQ notes article[1] for more information.

Hope that helps,
Mike
[1] <URL:http://www.jibbering.com/faq/faq_notes/square_brackets.html>


Yes, that really helps. Thanks a lot!! My code is readable again!


To pre-empt your next question "How do I see what's in the object?"

Here's how:

var item = {}
item[0] = 'foo';
item[1] = ['bar','buzz'];

var msg='Properties of object item:\n';
for ( prop in item) {
msg += '\n' + prop + ' has value ' + item[prop];
}

alert(msg);

Another interesting feature is that you can add methods to your
object. So you could add a method that shows what's in the object:

item.showContent = function() {
var msg=[];
for ( prop in item) {
if ( 'function' != typeof item[prop]) {
msg.push(prop + ' : ' + item[prop]);
}
}
alert(msg.join('\n'));
}

The 'typeof' test stops the method being printed out, comment it out
to see everything. To see what's in the object, call the method:

item.showContent();

--
Rob
Jul 23 '05 #5

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

Similar topics

11
by: propizzy | last post by:
Appreciate any help!!! PROBLEM: I have this form that allows the user to dynamically create additional fields (see javascript code bellow). I am trying to retrieve the values entered into these...
2
by: Tommy Lang | last post by:
Hi everybody! I am trying to learn the basics of C++ myself and have a hard time understanding some stuff like pointers and references etc. I have created a small program that adds two numbers...
1
by: Tommy Lang | last post by:
I am trying to learn to use dynamic variables. I have pasted the code below. Is this the proper way of using dynamic variables? Thanks, Tommy ...
4
by: Tim.D | last post by:
People, I've ventured into the wonderful world of Stored Procedures. My first experience has been relatively successful however I am stuck on using host variables to specifiy actualy table or...
1
by: Nathan Bloomfield | last post by:
Does anyone know if there is any documentation which relates to Access2k + ? or can anyone help adjust the code? I am having trouble converting the DAO references. TITLE :INF: How to...
28
by: Dennis | last post by:
I have a function which is called from a loop many times. In that function, I use three variables as counters and for other purposes. I can either use DIM for declaring the variables or Static. ...
12
by: scott | last post by:
Is there a way to create dynamic variables when looping through a recordset? For example below, after the 1st loop I'd have myVarA1 and myVarB1, after 2nd loop, I'd get myVarA2 and myVarB2. CODE...
2
by: deejayquai | last post by:
Hi I'm trying to produce a report based on a dynamic crosstab. Ultimately i'd like the report to actually become a sub report within a student end of year record of achievement. The dynamic...
2
by: JWL | last post by:
Hi I need to create a bunch of sites with slightly dynamic CSS. Basically, all the image paths in the CSS need to be dynamic, depending on the values of certain ASP variables. I can think of...
3
by: Mark S. | last post by:
As I understand it, C# doesn't offer dynamic variable names. Below is my attempted workaround. Is what I'm doing possible? FYI, I already read all the "why in the world do you need dynamic...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.