Randy Webb wrote:
Denis Perelyubskiy wrote:
<snip>
... . Please correct
me if I was wrong.
http://member.melbpc.org.au/~tgosbel...vascript-hash/
I think you misunderstood it.
<quote>
But in JavaScript, the index of an array need not be a number,
it can also be a word or key as listing 1 demonstrates.
</quote>
<snip>
When an article is headed "Real programming with JavaScript -
Associative Arrays" and then goes on to demonstrate a failure to
comprehend the javascript specification (ECMA 262) then maybe it is not
such a good place to go looking for advice/information on the subject.
First, there is no specific "array indexing" syntax in javascript. There
are only property accessors; dot notation property accessors and bracket
notation property accessors. Property accessors are used to reference
the named properties of objects (for value assignment, value reading and
deletion). However, because a dot notation property accessor may only
use legal identifiers, and a character sequence that resembles a number
cannot be a legal identifier, the referencing of the properties of an
array that represent the array elements can only be done with bracket
notation property accessors (where any character sequence can be used in
a property name).
When a bracket nation property accessor references a property of any
object the expression used within the brackets is type-converted in to a
string (as required by the property accessor algorithm: ECMA 262 3rd
edition Section 11.2.1). The nature and mechanism of this
type-conversion depends on the type of the value resulting form the
evaluation of the expression, as specified for the internal ToString
function (ECMA 262 3rd edition Section 9.8).
Thus, given the array - var a = ['x',.'y','.z']; -, the property
accessors - a[1] - and - a['1'] - are identical in their specified
behaviour.
A javascript array is no more than an ordinary javascript object (the
'native ECMAScript Object' that has undergone some internal augmentation
when it was created. Specifically, it has had its internal [[Prototype]]
property assigned a reference to - Array.prototype - (so that the Array
methods can be inherited), its internal [[Class]] property has been
assigned the string "Array", a '- length - property is created and
initialised (see ECMA 262 3rd edition Section 15.4.2.1-2 for
initialisation details) and (above all) its internal [[Put]] method is
replaced with the one that gives an Array object all of its special
characteristics. (note though that the internal [[Get]] method is
completely unchanged from the normal Object [[Get]] method).
The internal [[Put]] method takes a property name (string) and a value
as its arguments, and assigns the value provided to a property of the
object with the given name. The special [[Put]] method employed by Array
objects performs some additional steps when it is used to assign a value
to its object. The Array's [[Put]] method must be interested in the
assignments to the - length - property of an array, because if it is
less than the current value it may be necessary to delete properties
from the array. Otherwise, the property name string argument is
converted into a number using the internal ToUnit32 function, and if
that number is not less than the current value of the - length -
property then the - length - property of the Array is re-set to the
value of that number plus one.
Whenever the property name provided to the Array's [[Put]] method is not
a string representation of an (unsigned 32 bit) integer number clause 8
in the algorithm for the [[Put]] method avoids the need to consider
interaction with the Array's - length - property. Clause 8 is somewhat
vague in saying "If P is not an array index, return". But the
specification states that a property name string is an array index when
ToString(ToUnit32(propertyName)) equals propertyName, where 'equals'
means; exactly the same characters in exactly the same sequence (ECMA
262 3rd edition Section 15.4).
(It is interesting to note that the augmented object used as an Array
does not have a special [[Delete]] method assigned, so deleting an array
element has no specified effect on the Array's - length property.)
However, none of the above has anything to do with implementing
associative array or hashtable like behaviour in javascript as the
characteristics that distinguish an Array from any other native
ECMAScript Object relate to the handling of property names that satisfy
the definition of an "array index" and the array's length property.
Associative arrays and hashtable implementations employ a key-value pair
instead of array index. These implementations are taking advantage of
characteristics that are common to all native ECMAScirpt objects; the
ability to add named properties to any Object.
As a result, any discussion of implementing associative array or
hashtable like behaviour in javascript that commences with:-
var a = new Array();
- (or its equivalent) is written with a fundamental misconception (and
likely to promote confusion and misconceptions in its readers). In
reality, when name-value pairs are the only facility required of an
object then using an Array for that object will result in consequential
loss of performance because the Array's [[Put]] method has a more
involved algorithm and so will be slower in operation.
Richard.