Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Javascript (http://www.velocityreviews.com/forums/f68-javascript.html)
-   -   Array and Hash (Associative array) in JavaScript v.3.0 (http://www.velocityreviews.com/forums/t919427-array-and-hash-associative-array-in-javascript-v-3-0-a.html)

VK 07-28-2005 01:50 PM

Array and Hash (Associative array) in JavaScript v.3.0
 
Whatever you wanted to know about it but always were affraid to ask.

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


RobG 07-28-2005 02:40 PM

Re: Array and Hash (Associative array) in JavaScript v.3.0
 
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

VK 07-28-2005 03:32 PM

Re: Array and Hash (Associative array) in JavaScript v.3.0
 
> 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.


Kevin Newman 07-28-2005 04:12 PM

Re: Array and Hash (Associative array) in JavaScript v.3.0
 
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.

VK 07-28-2005 05:27 PM

Re: Array and Hash (Associative array) in JavaScript v.3.0
 
> 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.


Kevin Newman 07-28-2005 05:33 PM

Re: Array and Hash (Associative array) in JavaScript v.3.0
 
> 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.

Lasse Reichstein Nielsen 07-28-2005 05:42 PM

Re: Array and Hash (Associative array) in JavaScript v.3.0
 
"VK" <schools_ring@yahoo.com> 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 - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'

Kevin Newman 07-28-2005 06:06 PM

Re: Array and Hash (Associative array) in JavaScript v.3.0
 
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.

VK 07-28-2005 06:16 PM

Re: Array and Hash (Associative array) in JavaScript v.3.0
 
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?


VK 07-28-2005 06:22 PM

Re: Array and Hash (Associative array) in JavaScript v.3.0
 
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.



All times are GMT. The time now is 12:55 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.