Object Prototypes

A

Arthaey Angosii

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:

<html>
<head>
<title>Test</title>
<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);
}
</script>
</head>
<body onload="test();">
<pre id="bar">
<!-- this page's HTML code goes here, for demo/test case
purposes -->
</pre>
</body>
</html>

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?


Thanks,
Arthaey
 
A

Arthaey Angosii

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. :)
 

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

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top