473,686 Members | 2,836 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

VK
Whatever you wanted to know about it but always were affraid to ask.

<http://www.geocities.c om/schools_ring/ArrayAndHash.ht ml>

Jul 28 '05 #1
35 6637
VK wrote:
Whatever you wanted to know about it but always were affraid to ask.

<http://www.geocities.c om/schools_ring/ArrayAndHash.ht ml>


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.c om.au/group/comp.lang.javas cript/browse_frm/thread/c12423afa53a28f 8/589d140d9290e7a 9?q=array+lengt h+undefined&rnu m=7&hl=en#589d1 40d9290e7a9>

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
Jul 28 '05 #2
VK
> 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.c om/schools_ring/ArrayAndHash.ht ml#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.c om/schools_ring/ArrayAndHash.ht ml#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.

Jul 28 '05 #3
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 _NotifyListener s($newHash) {
for (var $key in $listeners)
$listeners[$key].Update($newHas h);
};

this.AddListene r = function($obj) {
if (!$obj.Update) return false;
$listeners[$listeners.leng th] = $obj;
if ($obj.Load)
$obj.Load($curr entHash);
return true;
};
this.RemoveList ener = 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.
Jul 28 '05 #4
VK
> NotifyListeners

Oh, I smell hot java! :-)
....
var $listeners = new Array();
function _NotifyListener s($newHash) {
for (var $key in $listeners) {
$listeners[$key].Update($newHa* sh);
}
}
....

This situation is described at:
<http://www.geocities.c om/schools_ring/ArrayAndHash.ht ml#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)obje ctTwo.someMetho d();
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.

Jul 28 '05 #5
> var $listeners = {};
would make it clear and natural.


That would make more sense I guess, but it would also eliminate the
$listeners.leng th 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.leng th
to generate one.)

Kevin N.
Jul 28 '05 #6
"VK" <sc**********@y ahoo.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.leng th] = $obj;


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

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 28 '05 #7
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.
Jul 28 '05 #8
VK
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.leng th] = $obj;


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

Full code of the constructor?

Jul 28 '05 #9
VK
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.

Jul 28 '05 #10

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

Similar topics

4
2681
by: Robert | last post by:
I am curious why some people feel that Javascript doesn't have associative arrays. I got these definitions of associative arrays via goggle: Arrays in which the indices may be numbers or strings, not just sequential integers in a fixed range. www.sunsite.ualberta.ca/Documentation/Gnu/gawk-3.1.0/html_chapter/gawk_20.html (n.) A collection of data (an array) where individual items can be indexed (accessed) by a string, rather than by...
5
6519
by: Denis Perelyubskiy | last post by:
Hello, I need to make an array of elements accross forms. My javascript skills, as evident from this question, are rather rudimentary. I tried to make an associative array and index it with the object references. However, I just realized that indices may only be referenced by strings.
14
6696
by: Yereth Jansen | last post by:
Hi all, I encountered a problem with looping through an associative array. All worked perfectly with the following code: for (var menuItem in this.menuItems) { doSomething(); } where this.menuItems is an associative array. The problem occurred when
47
5066
by: VK | last post by:
Or why I just did myArray = "Computers" but myArray.length is showing 0. What a hey? There is a new trend to treat arrays and hashes as they were some variations of the same thing. But they are not at all. If you are doing *array", then you have to use only integer values for array index, as it was since ALGOL.
21
21195
by: scandal | last post by:
I am a javascript newbie working on a script that checks whether a "path" from one element in an array to another is "blocked." Currently, the script pushes an already processed cell index (hence an integer) into an array. To prevent rechecking already processed cells, the script iterates through the (sorted) array to see whether that integer is an element of the array. After reading about javascript arrays a bit more, I thought...
22
4625
by: VK | last post by:
A while ago I proposed to update info in the group FAQ section, but I dropped the discussion using the approach "No matter what color the cat is as long as it still hounts the mice". Over the last month I had enough of extra proof that the cat doesn't hount mice anymore in more and more situations. And the surrent sicretisme among array and hash is the base for it. I summarized all points in this article:...
7
39843
by: Robert Mark Bram | last post by:
Hi All! How do you get the length of an associative array? var my_cars= new Array() my_cars="Mustang"; my_cars="Station Wagon"; my_cars="SUV"; alert(my_cars.length);
104
16953
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from that array Could you show me a little example how to do this? Thanks.
30
2935
by: josh | last post by:
Hi all, what does it meaning that strange sintax (look at the object :) ? if I have i.e. array.length I can use array. and is it IE/Firefox compatible??
5
2198
by: M. Fisher | last post by:
Pardon my ignorance here... I have created arrays such as: var SDET_Lab130= new Array(); SDET_Lab130 = new Array(); SDET_Lab130 = ; SDET_Lab130 = ; SDET_Lab130 = ; SDET_Lab130 = ; SDET_Lab130 = ;
0
8584
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8516
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
7599
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6440
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4308
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4532
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2945
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2205
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1934
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.