HighDots Forums  

arrays and objects

Javascript JavaScript language (comp.lang.javascript)


Discuss arrays and objects in the Javascript forum.



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

Default arrays and objects - 09-19-2003 , 01:44 PM







I am not a javascript guru or anything so I was wondering if someone
could tell me what the code below is doing. Is it creating a
multidimensional array? Would it be better to create an object?

function addKey(commentKey, codeHexa, gotoPage, funcToLoad)
{

aKey[nbrKey] = new Array();
aKey[nbrKey]["comment"] = commentKey;
aKey[nbrKey]["codeHexa"] = codeHexa;
aKey[nbrKey]["gotoPage"] = gotoPage;
aKey[nbrKey][0] = funcToLoad;
nbrKeys++;
}

Thanks

ABC

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

Default Re: arrays and objects - 09-20-2003 , 07:33 AM






"ABC" <bcampbell (AT) imagictv (DOT) com> wrote

Quote:
... I was wondering if someone could tell me what
the code below is doing.
The function creates a new Array and assigns it to a property of an
object (global) called - aKey - . Which is probably an Array itself as
it is indexed with the (global) variable - nbrKeys - which is probably a
number (integer) as it is subject to mathematical (post increment)
operations.

The function then creates a number of named properties of the array and
assigns values to them. JavaScript Arrays are objects (with extra
functionality added) and can have named properties added to them at any
point. Finally a value is assigned to the array element at index 0 and
the - nbrKeys - variable in incremented.

Quote:
Is it creating a multidimensional array?
Not really. JavaScritp does not do multidimensional arrays as such, it
can do Arrays of Arrays (which is similar, even practically identical in
some cases). Assuming that - aKey - is an Array to start with assigning
an Array to one of its elements will make it an Array of Arrays (but
values assigned elsewhere in the code cannot be determined form this
function example so the - aKey - array may contain references to any
objects of any type and any primitive values in its other elements).

Quote:
Would it be better to create an object?
snip

Impossible to say from just this code. As it is the new Array assigned
as an indexed element of - aKey - is having values assigned to its
indexed elements so it is possible that its Array-ness is being
exploited elsewhere in the code and an Array is the appropriate object
to be using.

Generally, if you want Array-like behaviour (indexing by integer) then
an Array is the appropriate object to use. If you want to index by name
only then a plain object would usually be better and if you want to do
both (as in this example) then you have a range of choices including
exploiting the Object-ness of an Array to provide named properties.

Richard.




Reply With Quote
  #3  
Old   
find_bailey@hotmail.com
 
Posts: n/a

Default Re: arrays and objects - 09-20-2003 , 11:16 AM



Thanks for that very detailed explanation, I realize it is sometimes
hard to answer a question with a small snippet of code. I am going
through a lot of javascript code trying to optimize a web application
running on an embedded web server, the web app has a lot of
javascript. The processor this web server is running on is slow and
handles floating point poorly(hard to explain that). If you have any
ideas or websites to visit to help with javascript code optimization
please let me know.

Again Thanks,

ABC



On Sat, 20 Sep 2003 13:33:34 +0100, "Richard Cornford"
<Richard (AT) litotes (DOT) demon.co.uk> wrote:

Quote:
"ABC" <bcampbell (AT) imagictv (DOT) com> wrote in message
news:3f6b4e45.4602648 (AT) allnews (DOT) nbnet.nb.ca...
... I was wondering if someone could tell me what
the code below is doing.

The function creates a new Array and assigns it to a property of an
object (global) called - aKey - . Which is probably an Array itself as
it is indexed with the (global) variable - nbrKeys - which is probably a
number (integer) as it is subject to mathematical (post increment)
operations.

The function then creates a number of named properties of the array and
assigns values to them. JavaScript Arrays are objects (with extra
functionality added) and can have named properties added to them at any
point. Finally a value is assigned to the array element at index 0 and
the - nbrKeys - variable in incremented.

Is it creating a multidimensional array?

Not really. JavaScritp does not do multidimensional arrays as such, it
can do Arrays of Arrays (which is similar, even practically identical in
some cases). Assuming that - aKey - is an Array to start with assigning
an Array to one of its elements will make it an Array of Arrays (but
values assigned elsewhere in the code cannot be determined form this
function example so the - aKey - array may contain references to any
objects of any type and any primitive values in its other elements).

Would it be better to create an object?
snip

Impossible to say from just this code. As it is the new Array assigned
as an indexed element of - aKey - is having values assigned to its
indexed elements so it is possible that its Array-ness is being
exploited elsewhere in the code and an Array is the appropriate object
to be using.

Generally, if you want Array-like behaviour (indexing by integer) then
an Array is the appropriate object to use. If you want to index by name
only then a plain object would usually be better and if you want to do
both (as in this example) then you have a range of choices including
exploiting the Object-ness of an Array to provide named properties.

Richard.



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

Default Re: arrays and objects - 09-21-2003 , 08:23 PM



<find_bailey (AT) hotmail (DOT) com> wrote

Quote:
Thanks for that very detailed explanation, I realize it is sometimes
hard to answer a question with a small snippet of code. I am
going through a lot of javascript code trying to optimize a web
application running on an embedded web server, the web app
has a lot of javascript. The processor this web server is running
on is slow and handles floating point poorly(hard to explain that).
If you have any ideas or websites to visit to help with javascript
code optimization please let me know.
You are not very clear about the set-up here. Is the JavaScript that you
wish to optimise client-side or server-side. I assume it is server-side
as otherwise the floating point performance on the server would not be
an issue. So is this JScript ASP or another server-side JavaScript?

In either case I probably cannot help much as I do my server side work
in Java and most of what I know about optimising JavaScript is related
to web browsers as an environment. But that doesn't mean that you won't
get help from someone if you provide some more detail.

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.