Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Array and Hash (Associative array) in JavaScript v.3.0

Reply
Thread Tools

Array and Hash (Associative array) in JavaScript v.3.0

 
 
VK
Guest
Posts: n/a
 
      07-28-2005
Whatever you wanted to know about it but always were affraid to ask.

<http://www.geocities.com/schools_ring/ArrayAndHash.html>

 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      07-28-2005
VK wrote:
> Whatever you wanted to know about it but always were affraid to ask.
>
> <http://www.geocities.com/schools_ring/ArrayAndHash.html>
>


I think you need to reconsider your comments about array length (my
wrapping of comments):

"var arrayObject = new Array(3); // arrayObject has 3 undefined
// elements"

That misconception has been repeated many times throughout your article.

The ECMA specification does not say that the length property is the
number of elements in the array, it is defined as being numerically
greater than the name of every property whose name is an array index
(which means it will be equal to the largest index plus 1 or greater).

For all practical purposes, it is irrelevant whether:

var x = new Array( 99 );

actually creates an array of 99 elements or not, but it does
explicitly create an array with a length property of 99. And that is
all you can say with certainty.

You appear to have disregarded the extensive conversation logged here
where the above was pointed out in great detail:

<URL:http://groups.google.com.au/group/comp.lang.javascript/browse_frm/thread/c12423afa53a28f8/589d140d9290e7a9?q=array+length+undefined&rnum=7&h l=en#589d140d9290e7a9>

Other conversations have recently covered similar ground.


An important property not mentioned by your page is that any element
with an index not less than the length will be deleted, so if you have
an array with length 10 and you set it to 1, any element with index of
1 or greater is deleted.



--
Rob
 
Reply With Quote
 
 
 
 
VK
Guest
Posts: n/a
 
      07-28-2005
> I think you need to reconsider your comments about array length (my
> wrapping of comments):
>
> "var arrayObject = new Array(3); // arrayObject has 3 undefined
> // elements"
>
> That misconception has been repeated many times throughout your article.


The misconception (or a plain stubborness) I'm trying to fight with has
been indeed discussed many times and its wrongness is demonstrated very
clearly in the article. I encourage you to go through again of:
<http://www.geocities.com/schools_ring/ArrayAndHash.html#Array_Length>
and below, as well as apply other array methods of you choice.

This misconception (let's stick to this softer term) erises from the
brute mix of the low level memory allocation and the high level
programming entity behavior.

As I may notice from your previous postings, the matrix transpoding is
your hobby(?). So especially for you it is vital to understand what are
you really working with and how will it respond to the applied methods.

> The ECMA specification does not say that the length property is the
> number of elements in the array, it is defined as being numerically
> greater than the name of every property whose name is an array index
> (which means it will be equal to the largest index plus 1 or greater).
>
> For all practical purposes, it is irrelevant whether:
>
> var x = new Array( 99 );
>
> actually creates an array of 99 elements or not, but it does
> explicitly create an array with a length property of 99. And that is
> all you can say with certainty.



<http://www.geocities.com/schools_ring/ArrayAndHash.html#Array_Length>
and further. Read the code samples in the grayed area. *Read it* , not
just pass over as "a method implementation error".


> An important property not mentioned by your page is that any element
> with an index not less than the length will be deleted, so if you have
> an array with length 10 and you set it to 1, any element with index of
> 1 or greater is deleted.



Yes, as well is if you assign arrayObject = [] then all elements will
be removed. I did not want to mention in a public reading that the
Array.length can be used as a brute force ReDim (alloc) method. What
you don't know will not hurt you Array has enough methods to
accomplish it more gracefully and reliably.

 
Reply With Quote
 
Kevin Newman
Guest
Posts: n/a
 
      07-28-2005
Interesting Article. I have been criticized for using the array type as
a hash (sort of) in a script that I created:

Test = new function() {

var $listeners = new Array();

function _NotifyListeners($newHash) {
for (var $key in $listeners)
$listeners[$key].Update($newHash);
};

this.AddListener = function($obj) {
if (!$obj.Update) return false;
$listeners[$listeners.length] = $obj;
if ($obj.Load)
$obj.Load($currentHash);
return true;
};
this.RemoveListener = function($obj) {
for (var $key in $listeners)
if ($listeners[$key] == $obj)
delete $listeners[$key];
};

...

};


I did this because it is convenient to use for ( in ) loops, and because
it is also convenient to use the length property to create a new key to
hold the added listener.

I wonder what you think about doing something like that. Is it abuse to
use an Array in this way?

Kevin N.
 
Reply With Quote
 
VK
Guest
Posts: n/a
 
      07-28-2005
> NotifyListeners

Oh, I smell hot java!


....
var $listeners = new Array();
function _NotifyListeners($newHash) {
for (var $key in $listeners) {
$listeners[$key].Update($newHa*sh);
}
}
....

This situation is described at:
<http://www.geocities.com/schools_ring/ArrayAndHash.html#ArrayAsHash>

Briefly *and in Java terms* you're creating an array object only to use
its super class methods and properties.
Visually it's equal to:

objectTwo = new objectOne();
and then continuosly
(objectOne)objectTwo.someMethod();
The natural question erises why did you create objectTwo on the first
place?

Or if we go back to JavaScript it's like:
var foo = false;
and few lines below:
foo = "Hello world!";

If you planned to use foo as string, why would you init it by boolean?

Is it an "abuse"? An abuse would be to use to force something to work
in the way it was not made for. So I would not say it's an "abuse".
Is it an unnecessary complication of your code readability? I would say
yes, for sure.
Is it a bad programming practice? Yes I guess.


var $listeners = {};
would make it clear and natural.

 
Reply With Quote
 
Kevin Newman
Guest
Posts: n/a
 
      07-28-2005
> var $listeners = {};
> would make it clear and natural.


That would make more sense I guess, but it would also eliminate the
$listeners.length property (I would use $listeners.push() but I'm
targeting IE 5.0), which I use in the AddListener method. Is there some
easy way to add an anonymous property to an object? (anonymous because I
don't have a name/id for it - which is why I'm using $listeners.length
to generate one.)

Kevin N.
 
Reply With Quote
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      07-28-2005
"VK" <(E-Mail Removed)> writes:

Remember attribution for your quotes.

> Briefly *and in Java terms* you're creating an array object only to use
> its super class methods and properties.


Not really, since he also does:

>> $listeners[$listeners.length] = $obj;


i.e., he is using integer indices, incrementally. Not something that
couldn't be done manually, though.

/L
--
Lasse Reichstein Nielsen - http://www.velocityreviews.com/forums/(E-Mail Removed)
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
 
Reply With Quote
 
Kevin Newman
Guest
Posts: n/a
 
      07-28-2005
Is what I've done considered hacky? It is quite convenient, but in your
opinion, should I use an integer variable that I increment manually (for
code readability)?

Kevin N.
 
Reply With Quote
 
VK
Guest
Posts: n/a
 
      07-28-2005
VK says:
>> Briefly *and in Java terms* you're creating an array object only to use
>> its super class methods and properties.

Lasse Reichstein Nielsen says:

> Not really, since he also does:
> $listeners[$listeners.length] = $obj;


This is actually where I stoke because this code should not work at
all. Unless somewhere below you do $listeners.length++ and *then* it's
an abuse. Otherwise $listeners.length is always 0.

Full code of the constructor?

 
Reply With Quote
 
VK
Guest
Posts: n/a
 
      07-28-2005
But overall if it works for you then fine.

Array used as Hash, Hash used as Array...

Is it correct? No. Does it work? Then leave it as it is.

 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
Benchmark segfault [Was: Array#inject to create a hash versus Hash[*array.collect{}.flatten] ] Michal Suchanek Ruby 6 06-13-2007 04:40 AM
Array#inject to create a hash versus Hash[*array.collect{}.flatten] -- Speed, segfault Anthony Martinez Ruby 4 06-11-2007 08:16 AM
Sort by hash vaule, an array of hash references fahdsultan@gmail.com Perl Misc 11 10-10-2005 09:35 PM



Advertisments