Connecting Tech Pros Worldwide Forums | Help | Site Map

Extend HTMLCollection.prototype with Firefox?

Matt Kruse
Guest
 
Posts: n/a
#1: Feb 2 '07
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?

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com



Richard Cornford
Guest
 
Posts: n/a
#2: Feb 2 '07

re: Extend HTMLCollection.prototype with Firefox?


On Feb 2, 4:58 am, Matt Kruse wrote:
Quote:
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.

RobG
Guest
 
Posts: n/a
#3: Feb 2 '07

re: Extend HTMLCollection.prototype with Firefox?



Richard Cornford wrote:
Quote:
On Feb 2, 4:58 am, Matt Kruse wrote:
Quote:
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.
Doesn't the line:

alert(x instanceof HTMLCollection); // true

indicate that it is an HTMLCollection?


--
Rob

Matt Kruse
Guest
 
Posts: n/a
#4: Feb 2 '07

re: Extend HTMLCollection.prototype with Firefox?


Richard Cornford wrote:
Quote:
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?
Quote:
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()?

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com


Richard Cornford
Guest
 
Posts: n/a
#5: Feb 3 '07

re: Extend HTMLCollection.prototype with Firefox?


Matt Kruse wrote:
Quote:
Richard Cornford wrote:
Quote:
>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.
HTMLCollection is an interface, just line NodeList.
Quote:
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.
Quote:
Quote:
>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
Good, as starting with something that is supposed to be an -
HTMLCollection - eliminates possible sources of interference.
Quote:
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.
Quote:
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.

Richard Cornford
Guest
 
Posts: n/a
#6: Feb 3 '07

re: Extend HTMLCollection.prototype with Firefox?


RobG wrote:
Quote:
Richard Cornford wrote:
Quote:
>>On Feb 2, 4:58 am, Matt Kruse wrote:
Quote:
>>Is it possible to extend the HTMLCollection prototype in
>>Firefox (>=2.0)? It looks like I can do it, but it doesn't
>>work:
<snip>
Quote:
Quote:
Quote:
>>window.onload= function() {
>> var x = document.getElementsByTagName('div');
<snip>
Quote:
Quote:
>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?
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.

Matt Kruse
Guest
 
Posts: n/a
#7: Feb 3 '07

re: Extend HTMLCollection.prototype with Firefox?


Richard Cornford wrote:
Quote:
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.
Quote:
Quote:
>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 -.
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).

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com


Richard Cornford
Guest
 
Posts: n/a
#8: Feb 5 '07

re: Extend HTMLCollection.prototype with Firefox?


Matt Kruse wrote:
Quote:
Richard Cornford wrote:
<snip>
Quote:
Quote:
>... . 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.
>
Quote:
Quote:
>>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 -.
>
Oops, what I meant was getElementsByTagName() of course.
Then that would be an object implementing the - NodeList - interface.
Quote:
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.

Closed Thread