
Signature
Anthony Jones - MVP ASP/ASP.NET
Anthony Jones wrote on 08 jul 2008 in
microsoft.public.scripting.jscript:
>> Collection of ids:
>> getElementById()
>
> No getElementById is a function. Creating a mental model that this is
> a collection is IMO detrimental to a clear understanding.
You could also call getElementById a string.
It calls a single element of the collection of ids in the document or in
part of that.
>> Object existing of a single element:
>> getElementById('myIdea')
>>
> Calling the function returns a value. The functions parameter
> specifies a key which the function uses internally to select a value
> to return.
That is implementation, if I understand your words.
In common words it returns [the pointer to] the element.
>> Collection of TagNames:
>> getElementsByTagName()
>
> getElementsByTagName is also function.
You are misreading I did not write "is".
It calls/returns a collection of a specified tagName out of the
collection of tagNames.
>> Object existing of all divs:
>> getElementsByTagName('div')
>>
> Return an array of values, in this case the values are HTML Div
> Element objects
Values are never objects, they are pointers to objects.
>> A single element of that object,
>> in itself also an object:
>> getElementsByTagName('div')[1]
>
> Get an array of HTML Div Element objects then use an array indexer [1]
> to retrieve the second entry in the array.
That was the whole point to give some logic to the difference between
xxx() for to get collections in general, a specified function indeed.
and
xxx[] to get an element of an array or an object.

Signature
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Anthony Jones - 08 Jul 2008 13:56 GMT
> Anthony Jones wrote on 08 jul 2008 in
> microsoft.public.scripting.jscript:
[quoted text clipped - 6 lines]
>
> You could also call getElementById a string.
You could call an elephant a horse if you wanted but I'm not quite sure why
you would want to. ;)
> It calls a single element of the collection of ids in the document or in
> part of that.
It returns the first element in the document that has an Id that matches the
parameter. It is not productive to think of it any more than that.
> >> Object existing of a single element:
> >> getElementById('myIdea')
[quoted text clipped - 6 lines]
>
> In common words it returns [the pointer to] the element.
The concept of a pointer is a C++ism that is not needed in JavaScript. The
behaviour of an object is defined and the fact that a variable is actually a
reference to an object is intrinsic. Unlike C++ you can't ever hold an
object 'by-value' in variable so there is no need to make such a
distinction.
(I'm having a Deja moment about having this same discussion re: this
distinction before).
> >> Collection of TagNames:
> >> getElementsByTagName()
[quoted text clipped - 5 lines]
> It calls/returns a collection of a specified tagName out of the
> collection of tagNames.
That is true this function does return a collection object.
> >> Object existing of all divs:
> >> getElementsByTagName('div')
[quoted text clipped - 3 lines]
>
> Values are never objects, they are pointers to objects.
As stated above such distinction is not really necessary, an "object value"
is implicit a reference to the object.
> >> A single element of that object,
> >> in itself also an object:
[quoted text clipped - 6 lines]
>
> xxx() for to get collections in general, a specified function indeed.
However your list of examples showed one function getting an element and
another function getting a collection (which is an object itself) of
objects. Both are just simply functions. You said earlier:-
"Collections define their elements inside (), "
They don't. () simply means call the function on LHS with the parameters
listed. As I described in another reply in this thread there is some
special handling of () in JScript where the LHS is an ActiveXObject but
neither of your examples are of that type, they are both simply function
calls.
Here is an example of the sort of collection access I think you were
refering to:-
var col = document.getElementsByTagName("TR");
alert(col("someid").innerText);
JScript converts col("someid") to a col.get_item("someid") function call
(item being the default property), of course JavaScript in something like FF
just errors saying col is not a function.
> and
>
> xxx[] to get an element of an array or an object.
Specifically [ ] means 'of the object supplied on the LHS access the
attribute specified'.
For example to the above example you could add:-
alert(col["length"]) which is equivalent to alert(col.length)
Similarly alert(col["1"].innerText) is equivalent to alert(col[1].innerText)
Also equivalent is alert(col.item(1).innerText) and alert(col(1).innerText)
The COM objects that are returned by MSHTML implement the IDispatchEx which
supports the implementation of [ ] on these objects.
Using [ ] on COM objects that do not implement IDispatchEx will return
undefined or will throw an error.

Signature
Anthony Jones - MVP ASP/ASP.NET