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.