469,328 Members | 1,316 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,328 developers. It's quick & easy.

Object Prototypes

I'm trying to use Object.prototype to define new functions on my
objects. I have a small test case HTML file online at
http://www.arthaey.com/test.html, also reproduced below:

<script type="text/javascript">
Object.prototype.foo = foo;
function foo() {return "Foo!";}
function test() {
alert("\n\nWINDOW: " + this.foo +
"\n\nPARA: " + document.getElementById("bar").foo);
<body onload="test();">
<pre id="bar">
<!-- this page's HTML code goes here, for demo/test case
purposes -->

It works in Firefox but I get the following Javascript error in
Firefox's Javascript console:

Error: [Exception... "'Error: No menu commaner found for URL:
http://www.arthaey.com/test.html' when calling method:
[nsIDOMEventListener::handleEvent]" nsresult: "0x8057001c
(NS_ERROR_XPC_JS_THREW_JS_OBJECT)" location: "<unknown>" data: no]

Then, in IE, this.foo is properly defined, but
document.getElementById("bar").foo is undefined. Is there some kind of
order-of-definition differences that causes bar's function to be
defined in one browser but not the other? Does anyone know what's
causing the Firefox error or how to fix it?

Aug 12 '05 #1
1 1412
I looked around a bit more online and and got some help from the
friendly #javascript folks. I tried using HTMLElement as the prototype
instead, but that didn't work any better.

Elsewhere in my real code, I have to look through all the elements that
I need functions added to, so I'm directly adding the functions to each
element, rather than mess with the prototypes.

Documented here for the benefit of future people running into this. :)

Aug 12 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by fb | last post: by
9 posts views Thread by Grumble | last post: by
13 posts views Thread by DevarajA | last post: by
19 posts views Thread by elzacho | last post: by
7 posts views Thread by junky_fellow | last post: by
5 posts views Thread by goldfita | last post: by
20 posts views Thread by Ari Krupnik | last post: by
73 posts views Thread by Steph Barklay | last post: by
reply views Thread by Purva khokhar | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.