The footnotes in this post go into more detail. I'd only read them if
you're comfortable with what you know about the subject. You might get
confused otherwise.
[snip]
So object doesn't have a length() method.
You mean length property.
Correct, an object doesn't have its own built-in length property.
And when you go obj[5] with an object, its not an index between the
brackets, its a property name?
Exactly.
Between the brackets you place an expression. It can be simple, like a
number or string, or something more complex like a function call or string
concatenation. This is how you can create property names at run-time.
The expression is evaluated and converted to a string[1], and the property
name is accessed.
The difference between arrays and other objects when assigning to a
property, is that it treats numbers as a special case[2]. Numeric indices
can update the length property of an array, but other names do not.
So you can iterate through the properties of an object much as you would
through a hash table?
For the most part, yes, with a for..in statement:
for(var propName in obj) {
/* propName is a string containing the property name.
* You can access the property value with obj[propName]
*/
}
The important part to note is that properties have attributes. You can't
actually modify these attributes, but they do exist.
There are four: ReadOnly, DontEnum, DontDelete, and Internal. The ability
to enumerate object properties with the code above is affected by the
second attribute, DontEnum. If it's present, you can access the property
directly, but the code above will skip it. Many built-in properties have
this attribute set. Properties you have *created* yourself will never have
this attribute set, so they can always be enumerated.
What are the differences, in Javascript, between an object and a hash
table that has pointers to properties and functions?
Depends on what particular implementation you're contemplating.
The biggest difference is that ECMAScript objects always contain certain
properties such as toString. You have to be careful not to confuse these
existing properties with actual keys.
If you're comparing to Java's Hashtable object, another big difference is
that it can use any object type (as long as it implements the hashCode and
equals methods) as a key, whereas property names are always strings in
ECMAScript.
Hope that helps,
Mike
[1] That part is important. Some hash tables allow you to use different
types as keys, which might lead you to believe that:
var obj = new Object();
var arr = new Array();
arr[obj] = ...;
allows you to assign values based on the actual object (I did once).
However, what happens is the object is converted to a string. As most
objects will simply return '[object Object]', or something similar, you
have to be careful here.
Also note that
arr[5] and arr['5']
are the same as the number will be converted to a string.
[2] It performs, what is effectively,
/* Convert name to string.
*
* e.g. arr[10], arr[0xa] and arr['10']
* all become '10'
*/
propertyName = String(propertyName);
/* If name, converted to a number then back to string,
* remains the same, treat as array index. If not, treat
* as property name.
*
* e.g. 1 arr[10], arr[0xa] and arr['10']
* all become '10'. Converted to a number and back
* to a string is still '10', and as '10' == '10',
* they are all treated as array indices.
* e.g. 2 arr['0xa'] to a number and back to a string
* is '10', but '0xa' != '10' so is a property, not
* not an index.
*/
if(String(Number(propertyName)) == propertyName) {
/* If the array index is greater than, or equal to, the
* length of the array, update the length property.
*/
if(Number(propertyName) >= array.length) {
array.length = Number(propertyName) + 1;
}
}