Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > <FAQENTRY> Array and hash (associative array)

Reply
Thread Tools

<FAQENTRY> Array and hash (associative array)

 
 
Michael Winter
Guest
Posts: n/a
 
      06-19-2005
On 19/06/2005 20:40, VK wrote:

> OK
> JavaScript array is not really an array.


That probably depends on the definition that you want to use. The
definition that I'm accustomed to is a homogeneous collection of
elements located sequentially in memory. However, more generic
definitions are just as acceptable.

What arrays in ECMAScript certainly are, are Object objects with
specialised behaviour. The same can be said for any native object.

> arrayObject.length keeps the highest index you managed to use so far.


The highest plus one.

> It has *no* connection with the actual array's length, it's just called
> so for further convenience.


I disagree. The array:

[, 1]

has two elements, and a length of two. Just because the first element is
undefined doesn't make the size any smaller. If you think it should,
then you are probably using the wrong data type.

[snip]

> Could we at least to mention in the FAQ's, that despite array and
> associative array (map, collection, hash table) is the same thing in
> JavaScript


OK, how many times does this need to be said: an array is not an
associative array, and an object is not an associative array. That might
be how they are implemented, and that's the sort of behaviour they
appear to possess, but that doesn't change the reality of the situation.

> if you do arrayObject[SomeNumber] = someValue, it counts it in
> arrayObject.length


If SomeNumber is a 32-bit unsigned integer, yes.

> It also lets you to use standard array methods on this member. (1st
> category)


The Array prototype methods generally operate in the range of named
properties [0, length), so that much is obvious. Some work outside that
range, namely when extending the number of elements.

> if you do arrayObject[SomeString] = someValue, the engine doesn't it in
> arrayObject.length


If SomeString represents an array index, then that assignment certainly
does affect the length. If SomeString is not an array index, then of
course it won't affect the length.

> and you cannot use standard array methods on this member. (2nd
> category)


For the previously cited reason.

> Until JavaScript/JScript engine producer will rectify this obvious bug,


It is not a bug. It is clearly defined behaviour.

> you need to be very attentive while putting new members in your
> arrayObject. [...]


One merely needs to understand the language. You clearly don't. I
suggest you get yourself a copy of ECMA-262 and read items 15.4 and
15.4.5.1, and compare the latter to 8.6.2.2.

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
 
Reply With Quote
 
 
 
 
Dr John Stockton
Guest
Posts: n/a
 
      06-19-2005
JRS: In article <(E-Mail Removed)>, dated Sun, 19 Jun 2005
18:09:37, seen in news:comp.lang.javascript, Lasse Reichstein Nielsen
<(E-Mail Removed)> posted :
>
>You can do that, and most of the time it will work fine for
>you. However, there will be the odd case where understanding how
>arrays work, in particular that the length property only guarantees
>to be at least one larger than the highest array index in use, is

^ integer ?
>necessary.



The prime point is the one I made before; that the length property does
not in the general case reflect the total number of elements, but is
associated with the number of non-negative-integer-index elements.

It is in fact associated with the way in which arrays work in Algol,
Pascal, and I presume C; and arrays in javascript are often used in that
manner, with integer indexing.

Once that is understood, it will sometimes be necessary to go into the
matter further.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
 
Reply With Quote
 
 
 
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      06-19-2005
Michael Winter <(E-Mail Removed)> writes:

> I disagree. The array:
>
> [, 1]
>
> has two elements, and a length of two.


I can see why Mr. VK gets confuzed, because I would say that this
array has one element and a length of two. It matters (to me) that
the array doesn't have a property called "0".

But then again, as Mr. VK also said, if I understood the Java
comparison correctly, arrays in Javascript is an abstraction on top of
an object. If you keep within the abstraction, you can treat the array
as having two elements. If you break the abstraction and uses the
array as a general object, then you need to know the implementation.

Safer languages protects their abstractions, Javascript is not one

> OK, how many times does this need to be said: an array is not an
> associative array, and an object is not an associative array. That
> might be how they are implemented, and that's the sort of behaviour
> they appear to possess, but that doesn't change the reality of the
> situation.


<devil's advocate>
What property of an associative array does an object fail to
match?
</devil's advocate>

>> Until JavaScript/JScript engine producer will rectify this obvious bug,

>
> It is not a bug. It is clearly defined behaviour.


Hear, hear.
I can't even see what the bug should be, except the failure to protect
the abstraction that is an array from its implementation.

/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
 
Richard Cornford
Guest
Posts: n/a
 
      06-19-2005
Lasse Reichstein Nielsen wrote:
<snip>
> Safer languages protects their abstractions, Javascript
> is not one


So let's not pretend it is safe, because it will turn and bite those who
assume it is.

<snip>
> <devil's advocate>
> What property of an associative array does an object
> fail to match?
> </devil's advocate>

<snip>

The common expectation of an "associative array" seems to be that the
number of key/value pairs assigned will be reflected in a - length -
property. Hence the disappointment when javascript does no such thing.

Richard.


 
Reply With Quote
 
Michael Winter
Guest
Posts: n/a
 
      06-19-2005
On 19/06/2005 22:18, Dr John Stockton wrote:

> JRS: In article <(E-Mail Removed)>, dated Sun, 19 Jun 2005
> 18:09:37, seen in news:comp.lang.javascript, Lasse Reichstein Nielsen
> <(E-Mail Removed)> posted :
>
>> [...] the length property only guarantees to be at least one larger
>> than the highest array index in use [...]

>
> ^ integer ?


Array indicies are a subset of object property names, and are only
defined in terms of 32-bit unsigned integers, so it would seem redundant.

[snip]

Could I ask a small favour of you, John? Can you check if IE4 supports
String.prototype.charCodeAt? Microsoft's documentation says it doesn't,
but my copy does. Though it is running on XP, it has a separate
jscript.dll library and should be independent from the system libraries.
It is with regard to other methods like Array.prototype.push.

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
 
Reply With Quote
 
Michael Winter
Guest
Posts: n/a
 
      06-19-2005
On 19/06/2005 23:18, Lasse Reichstein Nielsen wrote:

> Michael Winter <(E-Mail Removed)> writes:
>
>> I disagree. The array:
>>
>> [, 1]
>>
>> has two elements, and a length of two.

>
> [...] I would say that this array has one element and a length of two.


Whilst I still disagree, I do find it somewhat difficult to put into
words why. Perhaps it's my definition of what an array is, and why they
are normally used; a consequence of the notion of continuity, even if
the array is sparse.

> It matters (to me) that the array doesn't have a property called "0".


Although I understand that the property '0' doesn't exist in that

[, 1].hasOwnProperty('0')

returns false (interestingly, Firefox returns true), it still does have
a 0th element. It is a zero-indexed array, sparse or otherwise, so it
must have all of the elements in the range [0, length).

[snip]

> <devil's advocate>
> What property of an associative array does an object fail to
> match?
> </devil's advocate>


rf answered this one a while ago. In most ways, native objects do
resemble associative arrays. They possess both put and get operations,
and indicies are not limited to just numbers. However, a native object
is never empty. This renders get operations vulnerable to cases where a
prototyped property is returned as an element, which could include
arbitrary properties introduced by user code.

Though this problem could be mitigated by user code, the associative
array is now a user object, and not a native object.

[Regarding non-array index properties not affecting length]

> I can't even see what the bug should be [...]


I didn't mean for the summary about to answer the question, but that's
my understanding of it.

Mike


Does the subject of this thread need to be changed so that each post
isn't flagged as a potential FAQ entry?

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
 
Reply With Quote
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      06-20-2005
Michael Winter <(E-Mail Removed)> writes:

> It is a zero-indexed array, sparse or otherwise, so it must have all
> of the elements in the range [0, length).


So this is what it boils down to
Should a sparse datastructure be considered to have elements even
where they are not specified? I say no

> However, a native object is never empty. This renders get operations
> vulnerable to cases where a prototyped property is returned as an
> element, which could include arbitrary properties introduced by user
> code.


So the property it fails to have is the ability to be empty.
That's good enough for me

/L
--
Lasse Reichstein Nielsen - (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
 
VK
Guest
Posts: n/a
 
      06-20-2005
> Does the subject of this thread need to be changed
> so that each post isn't flagged as a potential FAQ entry?


Maybe, but how to call this new thread? Actually it's asking for
another
<FAQENTRY> title like "Does the world really exist or it's given to me
in my sensations and experience only?"

I don't really understant this attempt to alienate JavaScript
from any other language as it was written on Mars. It has the
most of the same entities as any other programming environment.

Any programming language is an abstraction. There is only two
real things: loaded/unloaded (1/0) memory cells and processor
flags after reading these cells. From this point of view there
is only one *real* language: ASSEMBLER (w/o mnemonics of course
as a disturbtion of the purity of the picture). Anything atope
of it as a *human abstraction* to desribe the movement and
allocation of these loads in the cells. There are not any objects,
arrays, hashes, variables etc. All from above is a kind of
"public agreement" in the meaning of Plato and Aristotle.
Nothing could us stop from deciding that say "array" would be
a data conglomerate where every second item starts with "A"
(or something even more bizzare).
On the OOP level any language consists of a set of objects,
where each object inherits/overloads/adds some method/properties
to the original Object (the Father of Gods). I don't see here
any major difference from any language. You can create a Data()
object in JavaScript and use it to play your media clips. You can
create a Array() object in JavaScript and use it to hold your
hash table. (Both by extending its properties or by changing
its prototype). Do you think that JavaScript is the most flexible
here? Some languages support operator's overload. So if you are
really bored at some sleepless night, you can make "+" sign to act
as "-" and vice versa. But do we need to state that Java
(that supports it) doesn't have a "+" sign but some "math object"
acting upon how do you use it?
If we stay at this point of view then there is no any fixed object,
and we're always acting with "something that can be anything" and the
only truth may come from our runtime experience. That puts us into
the most low level of the idealisme subjectif bordering with
a pure sensualisme.

IMHO You should respect profoundly the "public agreements"
about array and hash table (map, collection).
Otherwise you're the person who doesn't pay his bills on time
(because you're benefiting of something created 'cause of a public
agreement, but you don't want to follow that agreement right after).

In the particular, if you do var arrayObject = new Array();
you have to treat arrayObject as an array exclusively.
Otherwise why the hell did you create it?

 
Reply With Quote
 
John G Harris
Guest
Posts: n/a
 
      06-20-2005
In article <(E-Mail Removed) .com>, VK
<(E-Mail Removed)> writes

<snip>
>In the particular, if you do var arrayObject = new Array();
>you have to treat arrayObject as an array exclusively.
>Otherwise why the hell did you create it?


Is that what you're trying to say : don't misuse Array objects; if you
do you'll get into trouble. I wish you'd learn to say it more clearly.

John
--
John Harris
 
Reply With Quote
 
John G Harris
Guest
Posts: n/a
 
      06-20-2005
In article <(E-Mail Removed) .com>, VK
<(E-Mail Removed)> writes

<snip>
>On the OOP level any language consists of a set of objects,
>where each object inherits/overloads/adds some method/properties
>to the original Object (the Father of Gods).

<snip>

C++ doesn't have an 'original object' type, and is all the better for
it.

John
--
John Harris
 
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