Extend HTMLCollection.prototype with Firefox?

M

Matt Kruse

Is it possible to extend the HTMLCollection prototype in Firefox (>=2.0)?
It looks like I can do it, but it doesn't work:

HTMLCollection.prototype.funk = function() { alert(this.length); }
window.onload= function() {
var x = document.getElementsByTagName('div');
alert(HTMLCollection.prototype.funk); // function() { ... }
alert(x instanceof HTMLCollection); // true
alert(x.item===HTMLCollection.prototype.item); // false
x.funk(); // error: x.funk is not a function
}

Is there any other way to do it that will work right now?
 
R

Richard Cornford

Is it possible to extend the HTMLCollection prototype in Firefox (>=2.0)?
It looks like I can do it, but it doesn't work:

HTMLCollection.prototype.funk = function() { alert(this.length); }
window.onload= function() {
var x = document.getElementsByTagName('div');
alert(HTMLCollection.prototype.funk); // function() { ... }
alert(x instanceof HTMLCollection); // true
alert(x.item===HTMLCollection.prototype.item); // false
x.funk(); // error: x.funk is not a function

}

Is there any other way to do it that will work right now?

The - getElementsByTagName - method is specified as returning a
NodeList, but may in reality return a NamedNodeMap. If you want to
draw conclusions about objects implementing HTMLCollection shouldn't
you use a source that is specified as being one, such as -
document.forms -?

Richard.
 
R

RobG

Richard said:
The - getElementsByTagName - method is specified as returning a
NodeList, but may in reality return a NamedNodeMap.

Doesn't the line:

alert(x instanceof HTMLCollection); // true

indicate that it is an HTMLCollection?
 
M

Matt Kruse

Richard said:
The - getElementsByTagName - method is specified as returning a
NodeList, but may in reality return a NamedNodeMap.

I don't know much in this area, but I thought NodeList was the interface,
and that HTMLCollection was the implementation. I tried to extend NodeList
also, but I got the same results. Also, since (x instanceof HTMLCollection)
== true, I thought I was on the right track. If it doesn't return an
HTMLCollection object, why would that check be true?
If you want to
draw conclusions about objects implementing HTMLCollection shouldn't
you use a source that is specified as being one, such as -
document.forms -?

I just tried that as well with the same results.

Nevertheless, do you know which object, if any (except Object), I could
extend to add a method to the results returned by getElementById()?
 
R

Richard Cornford

RobG said:
Doesn't the line:

alert(x instanceof HTMLCollection); // true

indicate that it is an HTMLCollection?

It indicates that - HTMLCollection.prototype - is on the prototype chain
of - x -, assuming that as a host object - HTMLCollection - does not take
advantage of its option to provide a non-standard - [[HasInstance]] -
method. That does not even imply that the object - x - implements the -
HTMLCollection - interface.

Richard.
 
R

Richard Cornford

Matt said:
I don't know much in this area, but I thought NodeList was the
interface, and that HTMLCollection was the implementation.

HTMLCollection is an interface, just line NodeList.
I tried to extend NodeList also, but I got the same results.
Also, since (x instanceof HTMLCollection) == true, I thought
I was on the right track. If it doesn't return an HTMLCollection
object, why would that check be true?

The - instanceof - operator is not necessarily usefully discriminating
in javascript.
I just tried that

Good, as starting with something that is supposed to be an -
HTMLCollection - eliminates possible sources of interference.
as well with the same results.

Remember that - NodeList - and - NamedNodeMap - are specified (in the
ECMAScript bindings) as mapping bracket notation (and therefore, due to
ECMA 262 provisions, dot notation) property accessors onto their - item -
methods, and - HTMLCollection - is specified as mapping them onto its -
item - and - namedItem - methods. So your - x.funk - may be equivalent
to - x.namedItem('func') -, and as - namedItem - may only return an
object implementing the - Node - interface or null it should not be too
surprising if you don't get access to your method.
Nevertheless, do you know which object, if any (except Object),
I could extend to add a method to the results returned by
getElementById()?

Objects implementing the - Element - interface are returned by-
getElementById -.

Richard.
 
M

Matt Kruse

Richard said:
Remember that - NodeList - and - NamedNodeMap - are specified (in the
ECMAScript bindings) as mapping bracket notation (and therefore, due
to ECMA 262 provisions, dot notation) property accessors onto their -
item - methods, and - HTMLCollection - is specified as mapping them
onto its - item - and - namedItem - methods. So your - x.funk - may
be equivalent to - x.namedItem('func') -, and as - namedItem - may
only return an object implementing the - Node - interface or null it
should not be too surprising if you don't get access to your method.

That makes sense, I hadn't even considered that.
Objects implementing the - Element - interface are returned by-
getElementById -.

Oops, what I meant was getElementsByTagName() of course. Do you know of any
way to add methods to the prototype of the Object returned by this call in
Firefox? (Other than Object.prototype).
 
R

Richard Cornford

Matt said:
Richard Cornford wrote:

That makes sense, I hadn't even considered that.


Oops, what I meant was getElementsByTagName() of course.

Then that would be an object implementing the - NodeList - interface.
Do you know of any way to add methods to the prototype of the
Object returned by this call in Firefox? (Other than
Object.prototype).

I have never tried, but as - NodeList - maps property accessors to its -
item - method I don't expect you will successfully extend that object.

On the other hand, why? You can write a function that takes a -
NodeList - as an argument and do anything to/with that - NodeList - that
you could with a method.

Richard.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top